Skip to content

v0.15.0 Unenforced FK constraints

Compare
Choose a tag to compare
@marcgrue marcgrue released this 20 Dec 16:11
· 10 commits to main since this release

Using Molecule with sbt-molecule 1.11.0 no longer enforces foreign key constraints for SQL databases.

Power and orphans

This is a double-edged sword since we can then delete any entity we want now.

But if another entity references it, that reference id becomes an orphan reference pointing nowhere (to the deleted entity). So we get freedom to delete what we want at the cost of risking having orphaned reference ids hanging around.

Avoiding orphan refs

To avoid orphan ref ids, we can either

  • delete orphan ref ids manually,
  • add a foreign key constraint to our database schema or
  • not care

Adding a foreign key constraint

In the generated SQL schemas, you can copy any foreign key constraints you'd like to enforce and copy them to your live schema. Here's an example from an H2 schema:

// ALTER TABLE A ADD CONSTRAINT `_a` FOREIGN KEY (a) REFERENCES A (id);