Custom ForeignKey statement or altering Table create DDL

16 views
Skip to first unread message

Aurel Paulovic

unread,
Mar 20, 2017, 11:11:55 AM3/20/17
to Slick / ScalaQuery
Hi, I'm using Slick 3.2, slick-pg and PostgreSQL and I'm trying to convince Slick to create table with a foreign key index using "DEFERRABLE IMMEDIATELY DEFERRED".

I have a table defined in slick that looks something like this:
class MeasurementTable(tag: Tag) extends Table[(LocalDate, Int, Int)](tag, "measurements"){
 
def date = column[LocalDate]("date")
 
def instrumentId = column[Int]["instrumentId"]
 
def value = column[Int]("value")
 
def * = (date, instrumentId, value)

 
def _instruments = foreignKey("instruments_FK", instrumentId, TableQuery[InstrumentTable])(_.id, onUpdate = ForeignKeyAction.Cascade, onDelete = ForeignKeyAction.Cascade)
}

This creates a table that looks like this when defined in SQL in Postgres:
CREATE TABLE measurements (
 date date
,
 instrumentId
int,
 value
int,
 CONSTRAINT
"instruments_FK" FOREIGN KEY (instrumentId) REFERENCES instruments(id) ON UPDATE CASCADE ON DELETE CASCADE
);

What I want slick to create is this:
CREATE TABLE measurements (
 date date
,
 instrumentId
int,
 value
int,
 CONSTRAINT
"instruments_FK" FOREIGN KEY (instrumentId) REFERENCES instruments(id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
);

So I need basically to specify additional options on the foreign key index. This can be done by defining the foreign key with the additional options or by issuing an alter table query after the creation of the constraint like this:
ALTER TABLE measurements ALTER CONSTRAINT "instruments_FK" DEFERRABLE INITIALLY DEFERRED;

This should be handled transparently so that when somebody issues a create statement using TableQuery[MeasurementTable].schema.create it all just works, so it probably needs to somehow modify the DDL definition. Can it be done? How?

Thanks


Reply all
Reply to author
Forward
0 new messages