H2 not getting primary keys created #48

Closed
nafg opened this Issue Aug 13, 2009 · 5 comments

Projects

None yet

3 participants

@nafg
Contributor
nafg commented Aug 13, 2009

Run the following code


import root.net.liftweb.util._
import Helpers._
import root.java.sql.{Connection, DriverManager}

class M extends LongKeyedMapper[M] with IdPK {
def getSingleton = M
object field extends MappedString(this, 10)
}
object M extends M with LongKeyedMetaMapper[M]

object H2TestCase {
def main(args: Array[String]) {
DB.defineConnectionManager(DefaultConnectionIdentifier, new ConnectionManager {
def newConnection(name: ConnectionIdentifier): Box[Connection] = {
Class.forName("org.h2.Driver")
Full(DriverManager.getConnection("jdbc:h2:mem:lift;DB_CLOSE_DELAY=-1"))
}

  def releaseConnection(conn: Connection) {
      try {
          conn.close
      } catch {
          case e => Empty //ignore
      }
  }


})

Schemifier.schemify(true, Log.infoF _, M)

}

}

The output is:
INFO - CREATE TABLE m (field VARCHAR(10) , id BIGINT NOT NULL AUTO_INCREMENT)
INFO - CREATE INDEX m_id ON m ( id )

@dpp
Member
dpp commented Aug 13, 2009

Ummmm... what's the problem?

id BIGINT NOT NULL AUTO_INCREMENT

That seems to be the primary key

@nafg
Contributor
nafg commented Aug 13, 2009

Now I see that it is intentional - there is a comment to in Driver.scala that H2 creates a primary key when a column is AUTO_INCREMENT. I'm not so sure that's true though - looking into it now.

@nafg
Contributor
nafg commented Aug 18, 2009

I confirmed that this behavior was incorrect. Either the index should have a primary key constraint or it should use the PRIMARY KEY keywords in the CREATE TABLE statement. You can insert duplicate values into such columns. I went ahead and changed the flag in H2Driver so that it creates primary keys for you.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment