Skip to content

fix(db): repair model_relationships foreign keys broken by migration 22#9272

Merged
lstein merged 5 commits into
invoke-ai:mainfrom
Pfannkuchensack:fix/model-relationships-foreign-keys
Jun 24, 2026
Merged

fix(db): repair model_relationships foreign keys broken by migration 22#9272
lstein merged 5 commits into
invoke-ai:mainfrom
Pfannkuchensack:fix/model-relationships-foreign-keys

Conversation

@Pfannkuchensack

Copy link
Copy Markdown
Collaborator

Summary

Migration 22 rebuilds the models table by renaming it to models_old, creating a fresh models table, and dropping models_old. Modern SQLite (legacy_alter_table off) rewrites foreign-key references in other tables on rename, so the FKs in model_relationships were repointed at models_old, which was then dropped. This broke ON DELETE CASCADE and integrity for related models.

Add migration 32 to rebuild model_relationships with its foreign keys referencing models(id) again, preserving existing links and dropping any orphaned rows. Idempotent and a no-op when the FKs are already correct.

Related Issues / Discussions

fix for #8625

QA Instructions

  1. Use a database that has been through migration 22 (i.e. a model manager v3 upgrade). Its model_relationships foreign keys will reference the dropped models_old table — verify with:
    SELECT sql FROM sqlite_master WHERE name = 'model_relationships';
    -- before: FOREIGN KEY (...) REFERENCES "models_old"(id) ...
  2. Start Invoke so migration 32 runs (a DB backup is created automatically). Re-check the schema:
    SELECT sql FROM sqlite_master WHERE name = 'model_relationships';
    -- after: FOREIGN KEY (...) REFERENCES models(id) ...
  3. Confirm existing related-model links are still present, that orphaned rows (referencing deleted models) are gone, and that deleting a model now cascades to remove its relationship rows.
  4. Automated tests: pytest tests/app/services/shared/sqlite_migrator/migrations/test_migration_32.py (8 tests, covering FK repair, link preservation, orphan drop, cascade, index recreation, idempotency, and the no-table no-op case).

Merge Plan

This touches the DB schema and adds a new migration. Migration number 32 is currently free on main, but other in-flight branches also use 32/33 — coordinate the merge order and renumber if one of those lands first. No special release timing required beyond that; the migration auto-backs up the DB and is a no-op on databases whose foreign keys are already correct.

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • ❗Changes to a redux slice have a corresponding migration — N/A (no redux slice changes)
  • Documentation added / updated (if applicable) — N/A
  • Updated What's New copy (if doing a release after this PR)

Migration 22 rebuilds the `models` table by renaming it to `models_old`,
creating a fresh `models` table, and dropping `models_old`. Modern SQLite
(legacy_alter_table off) rewrites foreign-key references in other tables on
rename, so the FKs in `model_relationships` were repointed at `models_old`,
which was then dropped. This broke ON DELETE CASCADE and integrity for
related models.

Add migration 32 to rebuild `model_relationships` with its foreign keys
referencing `models(id)` again, preserving existing links and dropping any
orphaned rows. Idempotent and a no-op when the FKs are already correct.
@github-actions github-actions Bot added python PRs that change python files services PRs that change app services python-tests PRs that change python tests labels Jun 7, 2026
Pfannkuchensack added a commit that referenced this pull request Jun 12, 2026
…nst drift

- Declare the models/workflow_library GENERATED columns as SQLAlchemy Computed so
  create_all() reproduces them on MySQL/Postgres (json_unquote(json_extract(...)));
  no migration, no models-table rebuild (avoids re-breaking the #9272 FK fix).
- Add a schema-parity guard test that diffs the migration-built SQLite schema against
  SQLModel.metadata and compiles DDL for sqlite/mysql/postgres — fails on any future
  migration that models.py doesn't track.
- Close post-merge drift: image_subfolder (was crashing image save), delete_intermediates
  pairs, status_sequence, user_sessions/user_invitations tables.
- Use TEXT/LONGTEXT for JSON blobs; onupdate=utcnow on all updated_at columns; set
  session_queue status_sequence/started_at/completed_at app-side (no triggers on MySQL).
@lstein lstein self-assigned this Jun 17, 2026
@lstein lstein added the 6.13.5 Library Updates label Jun 17, 2026
@lstein lstein moved this to 6.13.5 LIBRARY UPDATES in Invoke - Community Roadmap Jun 17, 2026

@lstein lstein left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as advertised.

@lstein lstein enabled auto-merge (squash) June 24, 2026 01:27
@lstein lstein merged commit 407d24c into invoke-ai:main Jun 24, 2026
17 checks passed
@Pfannkuchensack Pfannkuchensack deleted the fix/model-relationships-foreign-keys branch June 26, 2026 12:07
dunkeroni pushed a commit to dunkeroni/InvokeAI that referenced this pull request Jun 29, 2026
…22 (invoke-ai#9272)

* fix(db): repair model_relationships foreign keys broken by migration 22

Migration 22 rebuilds the `models` table by renaming it to `models_old`,
creating a fresh `models` table, and dropping `models_old`. Modern SQLite
(legacy_alter_table off) rewrites foreign-key references in other tables on
rename, so the FKs in `model_relationships` were repointed at `models_old`,
which was then dropped. This broke ON DELETE CASCADE and integrity for
related models.

Add migration 32 to rebuild `model_relationships` with its foreign keys
referencing `models(id)` again, preserving existing links and dropping any
orphaned rows. Idempotent and a no-op when the FKs are already correct.

* Chore Ruff Format

---------

Co-authored-by: Lincoln Stein <lincoln.stein@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.13.5 Library Updates python PRs that change python files python-tests PRs that change python tests services PRs that change app services

Projects

Status: 6.13.5 LIBRARY UPDATES

Development

Successfully merging this pull request may close these issues.

2 participants