我创建了一个复合主键表:
class CreateWines < ActiveRecord::Migration[7.1]
def change
create_table :wines, primary_key: [:name, :producer_id] do |t|
t.string :name
t.timestamps
end
end
end
现在我想创建另一个具有一对多关系的表:
class CreateWineItems < ActiveRecord::Migration[7.1]
def change
create_table :wine_items do |t|
t.string :vintage
t.references :wine, null: false, foreign_key: true
end
end
我在db:migrate中收到错误:
PG::UndefinedColumn: ERROR: column "id" referenced in foreign key constraint does not exist
我在处理传递列和表的primary_key的引用:
class CreateWineItems < ActiveRecord::Migration[7.1]
def change
create_table :wine_items do |t|
t.string :vintage
t.references :wine, null: false, foreign_key: { column: [:name, :producer_id],
primary_key: [:name, :producer_id]}
t.timestamps
end
end
end
我发现了一个更具体的错误:
For composite primary keys, specify :column and :primary_key, where :column must
reference all the :primary_key columns from "wines"
如何正确地创建与具有复合主键的表的引用关联?
1条答案
按热度按时间a11xaf1n1#
references
不支持CPK(尚未)。你需要手动添加列和fkey,例如: