

So if that spec fails, don't add the column to The spec spec/db/schema_spec.rb will test if all columns with the _id suffix Reference an ID on a third party platform the _xid suffix is recommended. Only use this suffix for associations between two tables.

Naming foreign keysīy default Ruby on Rails uses the _id suffix for foreign keys. Not doing so will result in cascadingĭeletes being very slow. Thus you must also add a concurrent index. When adding a foreign key in PostgreSQL the column is not indexed automatically, Cascading DeletesĮvery foreign key must define an ON DELETE clause, and in 99% of the cases SeeĪdding foreign key constraint to an existing column. The method add_concurrent_foreign_keyĭoes not take care of this so you'll need to do so manually. Keep in mind that you can only safely add foreign keys to existing tables after Adding Foreign Keys In Migrationsįoreign keys can be added concurrently using add_concurrent_foreign_key asĭefined in Gitlab::Database::MigrationHelpers. User), instead of Rails having to do this. The database can very quickly remove associated data (for example, when removing a That data consistency is enforced on database level. Here you will need to add a foreign key on column er_id. ForĮxample, say you have the following model: class User < ActiveRecord :: Base has_many :posts end When adding an association to a model you must also add a foreign key.
