Repository navigation
feat: add on_delete_message and on_update_message options to references - #911
Merged
Merged
Conversation
…rences Deleting or updating a destination record that is still referenced returned a hardcoded "would leave records behind" error. The message can now be customised per reference with `on_delete_message` and `on_update_message`, falling back to the existing message when unset. Also flattens `add_related_foreign_key_constraints` so the constraint name and message are resolved once, matching `add_my_foreign_key_constraints`.
…reign keys Ecto uses the most recently added matching constraint, and since 1f6621b `add_configured_foreign_key_constraints` ran before `add_my_foreign_key_constraints` and `add_related_foreign_key_constraints`. Any `foreign_key_names` entry for a foreign key that a relationship already covered had its key and message shadowed. Configured foreign keys are now added last again.
Contributor
|
🚀 Thank you for your contribution! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Deleting a destination record (or updating its referenced attribute) while a foreign key still references it returned a hardcoded
"would leave records behind"error. There was no way to customise it: not on the reference, and not withforeign_key_nameseither.on_delete_message/on_update_messageThis adds two options to
reference:add_related_foreign_key_constraintsnow gets the action type (:delete, or:updatefor updates and upserts) and uses the matching message from the reference. If no message is set it falls back to"would leave records behind", so existing behaviour doesn't change.cases repeated the constraint registration four times. Now it works out the constraint name and message once, then has a singledefault_constraint_match_type/2case, the same shape asadd_my_foreign_key_constraints.foreign_key_namesoverrides were being shadowedEcto uses the most recently added matching constraint. Since
1f6621b8,add_configured_foreign_key_constraintshas run beforeadd_my_foreign_key_constraintsandadd_related_foreign_key_constraints. As a result, aforeign_key_namesentry for a foreign key that a relationship already covers has its key and message silently replaced by the derived one, on both create and delete. When the feature was introduced in830a4ca6, it ran last. This PR moves it back to the end of each branch, with a comment explaining why the order matters.Tests
test/sourced_foreign_key_test.exs: customon_delete_messageandon_update_message. This reusesSourcedFkParent/SourcedFkChild, with the parent'sidmade writable and an:updateaction added.test/update_test.exs: the default message when updating theidof a referencedPost. The delete default is still covered bytest/destroy_test.exs.test/references_test.exs:foreign_key_namesmessages take precedence on create (Permalink) and on destroy (Container). Both tests fail without the fix.