How to create a Foreign Key constraint using columnDefinition feature for MySQL?

374 views
Skip to first unread message

Dave McGee

unread,
Oct 11, 2011, 12:55:51 PM10/11/11
to ormlit...@googlegroups.com
Hi there,

Just wondering how to make use of this feature to manually define a constraint? 

  @DatabaseField(canBeNull = false, foreign = true, columnDefinition = "??")

Regards,
Dave.
 
---------- Forwarded message ----------
From: Gray Watson <256...@gmail.com>
Date: 11 October 2011 09:43
Subject: Re: foreign keys not created
To: ormlit...@googlegroups.com


On Oct 11, 2011, at 12:38 PM, Dave McGee wrote:

> However, when I do a "show create table" command on one of the tables - alarm, it doesn't show the foreign key references at a database level to reference the ID in the Resource table (Even though my Alarm class has a DatabaseField marked as foreign for Resource).

Aha. You are talking about foreign key _constraints_.

Yes, this is currently not supported by ORMLite.  Probably I should revisit this since we now effectively have foreign keys in the tables.  I'll add it to the TODO list.

With the new columnDefinition feature, you can certainly modify the generated SQL to add the constraints yourself.

       http://ormlite.com/docs/column-definition

gray




--
Regards,
Dave

Gray Watson

unread,
Oct 11, 2011, 1:06:04 PM10/11/11
to ormlit...@googlegroups.com
On Oct 11, 2011, at 12:55 PM, Dave McGee wrote:

> Just wondering how to make use of this feature to manually define a constraint?
> @DatabaseField(canBeNull = false, foreign = true, columnDefinition = "??")

Ooops, it may not be possible depending on the database type. For mysql:

http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

You would have to use an ALTER TABLE statement after the fact with dao.rawExecute("").

gray

Dave McGee

unread,
Oct 11, 2011, 1:06:51 PM10/11/11
to ormlit...@googlegroups.com
No problem, Thanks Gray!
--
Regards,
Dave
Reply all
Reply to author
Forward
0 new messages