AutoIncrement columns and objects

259 views
Skip to first unread message

Pete

unread,
Feb 26, 2012, 5:05:06 PM2/26/12
to ScalaQuery
Hi All,

If I have

case class foo(pkey : Int, value : String, value2 : String)
object example extends Table[foo] {
def pkey = column[Int]("pkey", O.AutoInc)
def value = column[String]("value")
def value2 = column[String]("value2")
def * = pkey ~ value ~ value2 <> (foo.apply _, foo.unapply _)
def ** = value ~ value2
def insert(value : String, value2 : String) = ** insert(value,
value2)
def softInsert(a : foo) = insert(a.value, a.value2)
}

then the database selects a pkey for me automatically if pkey is not
in the projection.
Much as projections are lovely it's quite nice to deal with objects
from case classes to represent rows.
However how do you store/deal with the primaryKey for objects that you
create and that haven't yet hit the database?

case class foo(pkey : Int = 0, value : String, value2 : String)

and catching the 0 is horrendous

case class foo(pkey : Option[Int] = None, value : String, value2 :
string)

Hits problems because then you have to allow nulls in your primary key
column....

case class foo(value : String, value2 : String)
case class fooFromDB(pkey : Int, value : String, value2 : string)
extends foo

If you ignore the case class inheritance issues can be made to work
but seems like a lot of work

Macias has already touched on this issue
https://github.com/szeiger/scala-query/issues/27

How are people dealing with this problem ?

Thanks

Pete

virtualeyes

unread,
Feb 27, 2012, 5:41:32 AM2/27/12
to ScalaQuery
Stefan (the author) uses a forInsert projection to address this issue
(look in test cases on git)

a PITA, really.

MySQL will accept null or 0 for autoincrement, so you can ugly hack it
that way with case classes.

I wouldn't expect any movement at all on this issue, ScalaQuery
project is not actively maintained in the sense that Stefan is full-
time working on SIQ, the evolution of ScalaQuery.

In other words, we have to make do with things as they are, good, but
not perfect ;-)
> Macias has already touched on this issuehttps://github.com/szeiger/scala-query/issues/27
Reply all
Reply to author
Forward
0 new messages