在我的rails应用程序中,我有多个postgresql模式。
SHOW search_path;
search_path
--------------
"$user",public,vehicle
我在车辆模式中有两个表(经销商、库存)。这种关系是这样的:
dealer has_many inventories
inventory belongs_to dealer
我创建了一个迁移,将关系添加为:
class AddDealerIdToVehicleInventories < ActiveRecord::Migration
def change
add_reference 'vehicle.inventories', :dealer, index: true, foreign_key: {on_delete: :cascade}
end
end
当我运行:rake db:migrate
时,这个迁移工作得很好,foreign_key似乎被添加到了表中,没有任何问题。但当我运行rake db:rollback
时,我会收到以下错误消息:
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedColumn: ERROR: column "vehicle.dealer_id" referenced in foreign key constraint does not exist
: ALTER TABLE "vehicle"."inventories" ADD CONSTRAINT "fk_rails_95ee16593d"
FOREIGN KEY ("vehicle.dealer_id")
REFERENCES "vehicle"."dealers" ("id")
ON DELETE CASCADE
我不确定我是否犯了任何错误,或者是rails迁移错误。谢谢你的帮助。
我通过将迁移文件更改为:来解决问题
class AddDealerIdToVehicleInventories < ActiveRecord::Migration
def up
add_reference 'vehicle.inventories', :dealer, index: true, foreign_key: {on_delete: :cascade}
end
def down
execute <<-SQL
ALTER TABLE vehicle.inventories
DROP COLUMN dealer_id
SQL
end
end
希望,这对其他人也有用。