-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal]: Make all reference constraints deferrable #42
Comments
Hey @volkanunsal, thanks for the proposal 👍 Can you add an example of a migration that should raise an error, and as well as one that does it properly? |
I don't quite know how to express the correct way of doing it. The way to alter a constraint requires knowing the name of the constraint, but that name is generated by Rails, and not easily accessible to the programmer. Here is an attempt to show what I mean: wrong class AddReferenceToProject < ActiveRecord::Migration[5.1]
def change
add_foreign_key :users, :projects
end
end right class AddReferenceToProject < ActiveRecord::Migration[5.1]
def change
# Naming our own foreign key. Is there a better way?
add_foreign_key :users, :projects, name: 'foobar'
execute("ALTER TABLE users ALTER CONSTRAINT foobar DEFERRABLE INITIALLY IMMEDIATE;")
end
end |
Hey @volkanunsal, thanks for following up. I don't know if this practice is common, so don't think it makes sense to add right now. I'd like to add support for custom rules at some point, which could allow you to create your own warning around this. |
fwiw, just added a way to write custom checks |
The foreign key constraints often raise issues when moving data, requiring the programmers to remove them to complete the data load tasks. A better approach is to make them deferrable (only in Postgres) and
SET CONSTRAINTS ALL DEFERRED;
in a transaction block before running your data load script.The proposed addition would warn the user if the migration includes a non-deferrable foreign_key constraint.
The text was updated successfully, but these errors were encountered: