Fix compilation error with migration file when CREATE UNIQUE INDEX referenced by FOREIGN KEY #732
Merged
AlecKazakova merged 9 commits intosqldelight:masterfrom Mar 18, 2026
Conversation
Migrations fix. When checking foreign key unique index constraints are valid, include SqlCreateIndexStmts. This will only work when the `CREATE UNIQUE INDEX` resides in the same file as the constraint table.
The FOREIGN KEY table can be either in the same file or external. The CREATE UNIQUE INDEX must be in the same file as the REFERENCES table
isCollectivelyUnique - don't pass source PsiElement otherwise this would filter out CreateIndexStmt elements
AlecKazakova
approved these changes
Mar 17, 2026
Contributor
Author
|
Thanks, needs 🧜 to merge as unable to in this project. |
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.
Fixes sqldelight/sqldelight#4503
Compile error
Table table_b does not have a unique index on columnsoccurs becauseCREATE UNIQUE INDEXis filtered out of the schema statements used in the check.This foreign key check could be an ⚔️ edge case where the schema contributor processing needs to include statements (index stmt) that are defined after the statement (table stmt) that triggers the lookup. By design, a statement cannot see tables, views defined later in the same file.
🔮 Attempt to fix migration files using
CREATE UNIQUE INDEXandFOREIGN KEYreferencesThis small fix on
containingFile.schema<SqlCreateIndexStmt>(this)to removethis(e.gtable_bin example below) allows all the schema statements in a containingFile to be accessible not just statements up-totable_b.This fix now allows a couple of options:
Single migration file - previously even this failed to compile
Split statements across migrations, however
table_bmust be in same file as unique index as only includes statements from files with a smaller migrationordervalue.More tables with FOREIGN KEY references
Note:
💥 The blast radius of this change is minimized to foreign key index checks and doesn't change the "by design" processing of schema in
com.alecstrong.sql.psi.core.SqlFileBaseCHANGELOG.md's "Unreleased" section has been updated, if applicable.