Support composite foreign keys via migration helpers #47637
Merged
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.
Closes #47593.
We discussed 3 possible ways of using
add_foreign_key.The first one (
add_foreign_key :brochures, :cars) (no:columnand:primary_keyoptions) turned out to be complex to implement, as it introduced a lot of kinda ugly changes in many places because we needed to properly generate these column names (consider configured table name prefixes and suffixes, pass a connection around to be able to query a primary key for theto_table. Some classes, likeForeignKeyDefinitiondoes not have access to the connection, and so we needed to calculate these columns at several places differently and pass there).And the biggest problem is when we need to add a foreign key for a self referencing tables, like
rails/activerecord/test/cases/migration/references_foreign_key_test.rb
Lines 221 to 225 in 1df6ad0
So I made that it is required to pass at least one of
:columnor:primary_keyoptions, as it is easy to infer one from the other.cc @eileencodes @nvasilevski