foreign key constraint in Lift's mapper
题
I created a table using Mapper in Scala.
class Stage extends Mapper[Stage]
{
def getSingleton = Stage
object controlId extends MappedLongForeignKey (this,Control) {
override def dbNotNull_? = true
override def dbColumnName = "control_id"
}
}
but my table does not create a constraint for the foreignKey key in database.
解决方案
Lift, like Rails, does not create foreign key constraints by default. If you want to enable them, you can do so via MapperRules
. Put this in your Boot
somewhere before you do other database initialization stuff.
MapperRules.createForeignKeys_? = (_) => true
The var createForeignKeys_?
is a function of type ConnectionIdentifier => Boolean
. I suppose this allows you to control foreign key creation per-connection, but most applications only have a single database connection.
不隶属于 StackOverflow