Repository navigation
Conversation
When only `on_delete` changes on a `ForeignKey`, the auto migration
contains `params={"on_delete": ...}`. `MigrationManager` drops the
foreign key constraint and adds it back with `on_update=None`, so the
new constraint has no ON UPDATE clause and Postgres uses NO ACTION. A
column that had ON UPDATE CASCADE loses it, and the same happens when
the migration is reversed. Changing only `on_update` resets
`on_delete` in the same way.
Take the rule that isn't in the migration from the column in the
schema snapshot.
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.
When only
on_deletechanges on aForeignKey, the auto migration contains just that key:MigrationManagerdrops the foreign key constraint and adds it back withon_update=None, so the new constraint has no ON UPDATE clause and Postgres uses NO ACTION. I ran it with Postgres 16. A newForeignKey(references=Manager)starts asON UPDATE CASCADE ON DELETE CASCADE. After the migration that switches it toOnDelete.set_null, it isON DELETE SET NULLonly, andUPDATE manager SET id = 100 WHERE id = 1now fails withviolates foreign key constraint "band_manager_fkey"instead of updating the band. Reversing the migration givesON DELETE CASCADEwithout the ON UPDATE clause. Changing onlyon_updatedoes the same toon_delete: withOnUpdate.restrictthe constraint ends up asON UPDATE RESTRICT, andON DELETE CASCADEis gone.The rule that isn't in the migration now comes from the column in the schema snapshot, which
_run_alter_columnsalready loads to find the constraint. A key that is missing fromparamsis also missing fromold_params, so it didn't change in that migration, and the snapshot value is right in both directions. With the change, the same steps giveON UPDATE CASCADE ON DELETE SET NULL, the UPDATE cascades to the band, and reversing restoresON UPDATE CASCADE ON DELETE CASCADE.test_alter_fk_on_delete_keeps_on_updatechanges onlyon_deletethe way an auto migration does, then checks both rules forwards and backwards. Without the fix it fails withAssertionError: OnUpdate.no_action != OnUpdate.cascade.tests/apps/migrationsandtests/columns/foreign_keypass on Postgres (201 passed, 5 skipped), andscripts/lint.shpasses. I did not run CockroachDB. The new test is marked for both engines like the existingtest_alter_fk_on_delete_on_update, so it would be good to see it pass in CI there.