Skip to content

Add support for foreign key constraints and foreign key actions#791

Open
sheaf wants to merge 1 commit intohaskell-beam:masterfrom
sheaf:foreign-key
Open

Add support for foreign key constraints and foreign key actions#791
sheaf wants to merge 1 commit intohaskell-beam:masterfrom
sheaf:foreign-key

Conversation

@sheaf
Copy link
Copy Markdown
Contributor

@sheaf sheaf commented Apr 4, 2026

This PR 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.
  • The function addTableForeignKey can be used to declare new foreign key constraints.

@sheaf sheaf force-pushed the foreign-key branch 2 times, most recently from 06f0ca8 to 1620314 Compare April 4, 2026 17:11
@LaurentRDC
Copy link
Copy Markdown
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

Copy link
Copy Markdown
Member

@LaurentRDC LaurentRDC left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! This is looking solid.

:: IsSql92ForeignKeyTableConstraintSyntax (BeamSqlBackendTableConstraintSyntax be)
=> ForeignKeySupport be
-- | The backend does not support @FOREIGN KEY@ constraints.
ForeignKeyUnsupported :: ForeignKeySupport be
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.
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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does the current design prevent adding this support in a later patch? What would be needed to get it working?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants