Foreign key is deleted in schema file after every migration

Viewed 163

I have this AchRelationship model:

class AchRelationship < ApplicationRecord
  belongs_to :account
end

And an Account model:

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

In my schema.rb file, I see this line, which makes sense to me:

add_foreign_key "ach_relationships", "accounts"

However, the issue I'm running into is that whenever I run rake db:migrate to add new migrations, that line in my schema.rb file gets deleted. It happens even when there are no new migrations. This happens with some members on my team but not all of us. With the other members, whenever they run rake db:migrate, they would add that line back if it was gone. So what ends up happening is that we keep seeing that line getting deleted and added back in our PRs and nobody was able to figure out why.

So my question is did I and some other team members do something weird when we set up our database? What can we do to fix this and prevent it from happening again? Thanks for reading!

1 Answers

Take a look here

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

It means someone on your team has a different database attribute/schema within that table ach_relationships. You can ask that person add the forign_key manually by migration or via the GUI tool. It actually happens all the time when someone did some migration in some branches and forgot to rollback it after the branch test.

Related