Could not get relations working

22 views
Skip to first unread message

Lei Zhao

unread,
Feb 25, 2019, 11:03:09 AM2/25/19
to Squeryl

I failed to commit the relation into database. However, `Book.printDdl` did print `alter table Book add constraint BookFK1 foreign key (AUTHOR_ID) references AUTHORS(id) on delete cascade`. I went into MySQL and found there was no constraint. This constraint was neither enforced by the squeryl, I can inserted item successfully into DB that apparently violates the constraint.

I've put my code here. https://github.com/LeeiFrankJaw/dir2db I hope someone can provide some insight.

Lei Zhao

unread,
Feb 25, 2019, 2:35:36 PM2/25/19
to Squeryl
I made some progress on this. Now I can confirm the `alter table` was not executed.  I set up the logger the according to http://squeryl.org/miscellaneous.html .

    Session.currentSession.setLogger(println)
   
Library.create
   
Library.printDdl

The output of `Library.create` differs from that of `Library.printDdl` in that it doesn't have the `alter table` statement.


Lei Zhao

unread,
Feb 26, 2019, 3:39:26 AM2/26/19
to Squeryl
It seems that MySQL is not supported for foreign key, as shown below.


  /**
   *
   * Foreign key constraints are not supported,
   *
   *  MySQL has some pre requisites for creating a foreign key constraint
   *  one of which is :
   *
   *  -> The foreign key can be self referential (referring to the same table). When you add a foreign key constraint to a table using ALTER TABLE, remember to create the required indexes first.
   *
   *
   *  Apparently there are other pre requisites, because creating foreign key constraints still gives :
   *
   * Time Action Response Duration / Fetch
   *  0 1 18:26:25 alter table CourseSubscription add constraint CourseSubscriptionFK3
   *  foreign key (courseId) references Course(id) Error Code: 1005
   *  Can't create table 'test.#sql-57c_42' (errno: 150)
   *
   * 
   *
   * 
   */


Lei Zhao

unread,
Feb 26, 2019, 4:58:35 AM2/26/19
to Squeryl
I replaced `MySQLAdapter` with `MySQLInnoDBAdapter`  and it works now and creates foreign key constraints for me.
Reply all
Reply to author
Forward
0 new messages