【发布时间】:2020-01-31 00:14:12
【问题描述】:
我有一个 Player 类,我想拥有 high_school_team 和 club_team 属性。那么我认为Player 将具有指向相应团队的high_school_team_id 和club_team_id 属性。我尝试在以下迁移中执行此操作,但它不起作用。
class CreatePlayers < ActiveRecord::Migration[6.0]
def change
create_table :players do |t|
t.string :first_name
t.string :middle_name
t.string :last_name
t.decimal :height
t.decimal :weight
t.date :birthday
t.references :team, :high_school_team, foreign_key: true
t.references :team, :club_team, foreign_key: true
t.decimal :gpa
t.string :class_year
t.string :intended_major
t.string :email
t.string :phone_number
t.text :notes
t.timestamps
end
end
end
它给出了以下错误:
code/scout-db [master●] » rails db:migrate
== 20191218003854 CreatePlayers: migrating ====================================
-- create_table(:players)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
you can't define an already defined column 'team_id'.
/Library/Ruby/Gems/2.6.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:372:in `column'
...
HighSchoolTeam 和ClubTeam 是与Team 进行单表继承的模型。
我不明白为什么会出现错误。 docs 似乎说t.referenes 的第一个参数是table_name 和第二个ref_name。 :team 是表的名称,我希望引用为 high_school_team_id 和 club_team_id。
当我将参数的顺序切换到t.references 时,它仍然不起作用。不知何故,它给出了同样的错误:you can't define an already defined column 'team_id'.。
【问题讨论】:
标签: ruby-on-rails rails-activerecord