【发布时间】:2016-11-18 04:08:27
【问题描述】:
我正在从一场比赛中创建一个新模型,其中包含一个主队和一个客队。 如果我运行 rake db:reset,它运行没有错误,但前三个字段(home_team、away_team 和 League)没有在数据库中生成,其他字段都可以。 这是我的迁移:
class CreateGames < ActiveRecord::Migration[5.0]
def change
create_table :games do |t|
t.references :home_team, references: :teams, foreign_key: true, index: true
t.references :away_team, references: :teams, foreign_key: true, index: true
t.belongs_to :league, foreign_key: true
t.integer :round
t.datetime :date
t.timestamps
end
end
end
这是生成的 schema.rb
create_table "games", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "round"
t.datetime "date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
但是,对我来说最奇怪的是我有另一个事务迁移,这很好用:
class CreateTransactions < ActiveRecord::Migration[5.0]
def change
create_table :transactions do |t|
t.references :from_user, references: :users, foreign_key: true, index: true
t.references :to_user, references: :users, foreign_key: true, index: true
t.decimal :amount
t.timestamps
end
end
end
【问题讨论】:
-
你应该用过
rake db:migrate -
可能表
teams不存在。尽量避免使用references,而是使用t.integer。 -
查看
add_reference的文档,我觉得references并不关心references: :teams选项。您能否指出我正在关注的文档?
标签: ruby-on-rails ruby activerecord ruby-on-rails-5 rails-migrations