Rails Postgres Schema 引用回滚问题
Rails Postgres Schema references rollback issue
在我的 rails 应用程序中,我有多个 postgresql 模式。
SHOW search_path;
search_path
--------------
"$user",public,vehicle
我在车辆架构中有两个 table(经销商、库存)。这种关系是这样的:
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 似乎毫无问题地添加到 table。但是当我 运行 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
希望这对其他人也有用。
在我的 rails 应用程序中,我有多个 postgresql 模式。
SHOW search_path;
search_path
--------------
"$user",public,vehicle
我在车辆架构中有两个 table(经销商、库存)。这种关系是这样的:
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 似乎毫无问题地添加到 table。但是当我 运行 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
希望这对其他人也有用。