4

Using the API SQL Builder com.healthmarketscience.sqlbuilder.

DbForeignKeyConstraint constraint = dbTable.foreignKey("fk_" + tableName + "_" + foreignTableName,
        columnList.toArray(new String[0]),
        foreignTableName,
        foreignColumnList.toArray(new String[0]));

how to set the constraint so that it does ON UPDATE CASCADE and ON DELETE CASCADE?

theAnonymous
  • 1,701
  • 2
  • 28
  • 62
  • 1
    A dirty hack `... new String[] {foreignColumnList.get(0)+ " ON UPDATE CASCADE});` might just work, but an additional `ALTER TABLE ... ON UPDATE CASCADE` might be vastly more readable. Ask the library makers for an addition. – Joop Eggen Feb 17 '20 at 11:35

2 Answers2

2

To the best of my understanding, the library you're using (com.healthmarketscience.sqlbuilder) doesn't allow to declare a foreign key constraint with ON UPDATE/DELETE CASCADE. I've checked the code, and this functionality isn't implemented.

The reason for this could be that the authors of the library have not implemented this functionality yet, or that they don't intend to implement it for some reason. You should be able to reproduce the functionality with a TRIGGER, but again the llibrary doesn't seem to support those.

If this functionality is crucial to your application, you should probably switch to a library that allows for it.

mtrycz
  • 91
  • 2
0

For future readers, this functionality has been added to SQLBuilder in version 3.0.1. Once you generate the ForeignKeyConstraintClause instance, you can call setOnDeleteAction(ReferentialAction.CASCADE) (and same for update action).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118