每次迁移后,架构文件中的外键都会被删除

Foreign key is deleted in schema file after every migration

我有这个 AchRelationship 型号:

class AchRelationship < ApplicationRecord
  belongs_to :account
end

还有一个 Account 模型:

class Account < ApplicationRecord
  has_one :ach_relationship, dependent: :destroy
end

在我的 schema.rb 文件中,我看到这一行,这对我来说很有意义:

add_foreign_key "ach_relationships", "accounts"

但是,我 运行 遇到的问题是,每当我 运行 rake db:migrate 添加新的迁移时,我 schema.rb 文件中的那一行就会被删除.即使没有新的迁移,它也会发生。这种情况发生在我团队中的一些成员身上,但不是我们所有人。对于其他成员,每当他们 运行 rake db:migrate 时,如果该行消失了,他们就会将其添加回去。所以最终发生的是,我们不断看到该行被删除并重新添加到我们的 PR 中,但没有人能够弄清楚原因。

所以我的问题是我和其他一些团队成员在设置数据库时是否做了一些奇怪的事情?我们可以做些什么来解决这个问题并防止它再次发生?感谢阅读!

看看here

Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.

这意味着您团队中的某个人在 table ach_relationships 中拥有不同的数据库 attribute/schema。您可以要求该人通过迁移或通过 GUI 工具手动添加 forign_key。其实经常有人在某些分支做了一些迁移,分支测试后忘记回滚了。