Search code examples
h2ddlscalaqueryh2db

Why doesn't ScalaQuery create ddl for foreign key?


I have the following table definitions using ScalaQuery 0.10.0-M1:

import org.scalaquery.ql.basic.{ BasicTable => Table }

object Nodes extends Table[(String, String)]("node") {
    def id = column[String]("id", O.PrimaryKey)
    def name = column[String]("name", O.NotNull)

    def * = id ~ name
    private def uName = index("uk_name", name, unique = true)
}

object Links extends Table[(String, String, String, String)]("link") {
    def id = column[String]("id", O.PrimaryKey)
    def from = column[String]("from_id", O.NotNull)
    def link = column[String]("name", O.NotNull)
    def to = column[String]("to_id", O.NotNull)

    def * = id ~ from ~ link ~ to
    private def ukFromLinkTo = index("uk_FromLinkTo", from ~ link ~ to, unique = true)
    private def fkFrom = foreignKey("fk_Link_Node_From", from, Nodes)(_.id)
    private def fkTo = foreignKey("fk_Link_Node_To", to, Nodes)(_.id)
}

But when creating (and printing) the ddl using this snippet:

val db = Database.forURL("jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")

db withSession {
    val ddl = (Nodes.ddl ++ Links.ddl)
    ddl.create
    println(ddl.createStatements.mkString("\n"))
}

No Foreign Keys get generated nor printed.

Why is that? And how do I fix it?


Solution

  • It looks like it may be because your index and foreignKey methods are private. I found this question as part of a search on unique columns with scalaquery. I already had a non-private index method and copied one of your fk methods and found that the index was being created, but not the fk constraint. When I removed the "private" keyword the fk constraint was created.