Hello,
In a small application that I am trying to build (https://github.com/codingkapoor/simple-lms-platform), I am using slick to build read side persistence.
In order to pace up my development, I use Lagom’s “setGlobalPrepare” method on ReadsideHandler builder to create tables on mysql for me on startup.
.setGlobalPrepare(
employeeRepository.createTable andThen
intimationRepository.createTable andThen
requestRepository.createTable
)
But to my surprise if I use createIfNotExists
api on schema to create tables, the foreign key constraints are not getting created. It is only if I change it to create
are the foreign key constraints getting created.
simple-lms-platform/employee-impl/src/main/scala/
com/codingkapoor/employee/persistence/read/dao/intimation/
IntimationRepository.scala
simple-lms-platform/employee-impl/
src/main/scala/com/codingkapoor/employee/persistence/read/dao/intimation/
IntimationTableDef.scala
// doesn't create foreign key constraints
def createTable: DBIO[Unit] = intimations.schema.createIfNotExists
// creates foreign key constraints
def createTable: DBIO[Unit] = intimations.schema.create
Which I verfiy on mysql like so:
mysql> show create table intimation
-> ;
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| intimation | CREATE TABLE `intimation` (
`EMP_ID` bigint(20) NOT NULL,
`REASON` text NOT NULL,
`LATEST_REQUEST_DATE` date NOT NULL,
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
mysql> show create table intimation;
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| intimation | CREATE TABLE `intimation` (
`EMP_ID` bigint(20) NOT NULL,
`REASON` text NOT NULL,
`LATEST_REQUEST_DATE` date NOT NULL,
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`),
KEY `EMP_FK` (`EMP_ID`),
CONSTRAINT `EMP_FK` FOREIGN KEY (`EMP_ID`) REFERENCES `employee` (`ID`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
Please let me know if this is a bug or am I making any mistakes here. Thanks