Skip to content

Keep on_update when a migration only alters on_delete - #1453

Open
PDGGK wants to merge 1 commit into
piccolo-orm:masterfrom
PDGGK:fix-fk-on-update-reset
Open

PDGGK wants to merge 1 commit into
piccolo-orm:masterfrom
PDGGK:fix-fk-on-update-reset

Conversation

@PDGGK

@PDGGK PDGGK commented Nov 11, 2026

Copy link
Copy Markdown

When only on_delete changes on a ForeignKey, the auto migration contains just that key:

manager.alter_column(
    ...
    params={"on_delete": OnDelete.set_null},
    old_params={"on_delete": OnDelete.cascade},
    column_class=ForeignKey,
    old_column_class=ForeignKey,
)

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. I ran it with Postgres 16. A new ForeignKey(references=Manager) starts as ON UPDATE CASCADE ON DELETE CASCADE. After the migration that switches it to OnDelete.set_null, it is ON DELETE SET NULL only, and UPDATE manager SET id = 100 WHERE id = 1 now fails with violates foreign key constraint "band_manager_fkey" instead of updating the band. Reversing the migration gives ON DELETE CASCADE without the ON UPDATE clause. Changing only on_update does the same to on_delete: with OnUpdate.restrict the constraint ends up as ON UPDATE RESTRICT, and ON DELETE CASCADE is gone.

The rule that isn't in the migration now comes from the column in the schema snapshot, which _run_alter_columns already loads to find the constraint. A key that is missing from params is also missing from old_params, so it didn't change in that migration, and the snapshot value is right in both directions. With the change, the same steps give ON UPDATE CASCADE ON DELETE SET NULL, the UPDATE cascades to the band, and reversing restores ON UPDATE CASCADE ON DELETE CASCADE.

test_alter_fk_on_delete_keeps_on_update changes only on_delete the way an auto migration does, then checks both rules forwards and backwards. Without the fix it fails with AssertionError: OnUpdate.no_action != OnUpdate.cascade. tests/apps/migrations and tests/columns/foreign_key pass on Postgres (201 passed, 5 skipped), and scripts/lint.sh passes. I did not run CockroachDB. The new test is marked for both engines like the existing test_alter_fk_on_delete_on_update, so it would be good to see it pass in CI there.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant