Repository navigation
Propagate rename_table to inbound ForeignKey references in the schema snapshot - #1436
Open
regiscamimura wants to merge 1 commit into
Open
regiscamimura wants to merge 1 commit into
regiscamimura wants to merge 1 commit into
Conversation
… snapshot Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018o8TvRZFEEBXJWXvR1ikd9
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.
The bug
piccolo migrations new <app> --autodiffs the current tables against aSchemaSnapshotreplayed from the migration history. When the history contains arename_table, the snapshot renames that table — but leaves everyForeignKeyon other tables still pointing at the old table class (params["references"]). The live models reference the new class, andSerialisedTableTypecompares by"{tablename}-{ClassName}", so every subsequent--autoemits analter_column(references=New, old_params={"references": Old})for each inbound FK, forever.Repro from history:
In our app, a table rename in August produced the same nine phantom
alter_columns in every one of the ten migrations generated since, each stripped by hand. (They execute no SQL —alter_columnnever acts onreferences— so committing them once also works around it downstream, which is how we found the cause.)The fix
In
SchemaSnapshot.get_snapshot, after applying arename_table, walk every table'sForeignKeycolumns and replace areferencesthat names the old class/tablename with a subclass carrying the new class name and tablename (viacreate_table_class), updating both_meta.params["references"]and_foreign_key_meta.references.Test
test_rename_table_updates_foreign_key_references— asserts the snapshot's FK now names the new class and tablename, and that aDiffableTablebuilt from the renamed model diffs to noalter_columns. Fails onmaster("Band" != "Performer"), passes with the fix.tests/apps/migrations: 79 passed, 84 skipped (SQLite). black / isort / flake8 / mypy clean on the two files.🤖 Generated with Claude Code
https://claude.ai/code/session_018o8TvRZFEEBXJWXvR1ikd9