Search code examples
sqlscalajdbcslickslick-2.0

Slick - drop table with unknown foreign key constraints


I have an application which creates its tables as needed on format changes. When the tables do not pass a simple functionality test, they are dropped and created again. This has worked great so far, but now I have changed some constraints: I have changed a primary key and added a foreign key.

The drop now fails with exception:

org.h2.jdbc.JdbcSQLException: Constraint "PERSON_ID" not found; SQL statement:
alter table "GARDEN" drop constraint "PERSON_ID" [90057-178]

The drop statement is as follows:

alter table "GARDEN" drop constraint "PERSON_ID"
alter table "GARDEN" drop constraint "PRIMARY_KEY"
drop table "GARDEN"

The trouble is this drop statement would be valid for the desired table format, but is invalid for the format used before (which is the format of the table which was detected as unusuable and triggered the drop to be done).

How can I drop the tables with no regard to the constraints? (I am always dropping all tables which are related - therefore I do not need the constraint checking in this case)


Solution

  • If you tell Slick there are constraints then Slick will try to drop them when dropping the table. You can use plain SQL to explicitly run the SQL statements you need. You can also use Slick2's createModel in the driver and check for constraints and that way conditionally do things.