Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that migrations that rename foreign key columns should use immediate foreign key checks #1740

Open
groue opened this issue Mar 4, 2025 · 0 comments

Comments

@groue
Copy link
Owner

groue commented Mar 4, 2025

... so that SQLite updates the foreign key.

It looks like SQLite automatically migrates foreign keys when tables are renamed, but not when columns are renamed (documentation).

I recently had to rename a foreign key, and I had to run the migration with immediate foreign key checks in order to avoid integrity failures:

// Use immediate foreign key checks so that the foreign key is properly migrated.
migrator.registerMigration("v8", foreignKeyChecks: .immediate) { db in
    try db.rename(table: "foo", to: "bar")
    try db.alter(table: "qux") { t in
        t.rename(column: "fooId", to: "barId")
    }
}

This must be documented, because it is far from obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant