Add support for foreign key constraints and foreign key actions#791
Open
sheaf wants to merge 1 commit intohaskell-beam:masterfrom
Open
Add support for foreign key constraints and foreign key actions#791sheaf wants to merge 1 commit intohaskell-beam:masterfrom
sheaf wants to merge 1 commit intohaskell-beam:masterfrom
Conversation
06f0ca8 to
1620314
Compare
Member
|
I am really swamped right now so I won't be able to review this for some time. Hopefully someone else can take a look |
LaurentRDC
reviewed
Apr 10, 2026
Member
LaurentRDC
left a comment
There was a problem hiding this comment.
Thanks for your contribution! This is looking solid.
| :: IsSql92ForeignKeyTableConstraintSyntax (BeamSqlBackendTableConstraintSyntax be) | ||
| => ForeignKeySupport be | ||
| -- | The backend does not support @FOREIGN KEY@ constraints. | ||
| ForeignKeyUnsupported :: ForeignKeySupport be |
Member
There was a problem hiding this comment.
I'm actually curious if there are any SQL DBMs that do not support foreign keys.
I know some backends (e.g. SQLite) may ignore these constraints, but I wouldn't say they're unsupported.
This commit adds support for foreign key constraints and actions to
`beam-migrate`, with support in the Postgres and SQLite backends:
- The `ForeignKeyAction` datatype is used to represent possible
actions to perform when updating or deleting a row with referencing
foreign keys.
- `createTableActionProvider` now takes an additional
`ForeignKeySupport` argument: a witness of evidence for
`IsSql92ForeignKeyTableConstraintSyntax` for backends that
support it.
- Postgres and SQLite support for parsing existing foreign key
constraints.
- Introduce the `addTableForeignKey` function for declaring new
foreign key constraints.
LaurentRDC
reviewed
Apr 11, 2026
Comment on lines
+412
to
+414
| -- Note: this does not support cycles in the foreign key reference graph, | ||
| -- for which we would need ALTER TABLE ADD CONSTRAINT, which we don't | ||
| -- currently support. |
Member
There was a problem hiding this comment.
Does the current design prevent adding this support in a later patch? What would be needed to get it working?
Contributor
Author
There was a problem hiding this comment.
I don't think the current patch would prevent that enhancement. I believe we would need to:
- add a new typeclass, with an instance for Postgres,
- update this logic to allow declaring foreign key constraints that refer to tables that don't exist yet; it would need to branch on whether ALTER TABLE ADD CONSTRAINT is supported, which seems like it would require another GADT like
ForeignKeySupport - add a provider that generates the appropriate ALTER TABLE syntax for foreign key constraints that remain unfulfilled
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.
This PR adds support for foreign key constraints and actions to
beam-migrate, with support in the Postgres and SQLite backends.ForeignKeyActiondatatype is used to represent possible actions to perform when updating or deleting a row with referencing foreign keys.createTableActionProvidernow takes an additionalForeignKeySupportargument: a witness of evidence forIsSql92ForeignKeyTableConstraintSyntaxfor backends that support it.addTableForeignKeycan be used to declare new foreign key constraints.