renaming KeyedEntity.id

441 views
Skip to first unread message

bwbecker

unread,
Jun 6, 2011, 10:00:15 AM6/6/11
to Squeryl
I'd like to use Squeryl (great tool, by the way!) with an existing
database. Primary keys
are NOT named "id", which seems to be assumed by Squeryl's update
facility.

Here is the best I've been able to manage to make it look like id has
a different name in
the scala code. I'd like to have the names in the scala code match
the actual database
column names. With this approach I can't assign to the primary key,
but otherwise I
think I can pretty much ignore "id" and use "auth_id".

Are there any other approaches that would work better? Anything in
this approach that
I've overlooked and is going to bite me?

I saw in another post the use of

on(yourTable)(e => declare(
e.id is(primaryKey)
))

but update doesn't seem to work if id is renamed to something else.

Thanks,
Byron



class Author(
@Column("auth_id")
val id: Long,
var firstName: String,
var lastName: String,
var email: Option[String]) extends KeyedEntity[Long]
{
def auth_id = id
}

class Book(
@Column("book_id")
val id: Long,
val title: String,
val auth_id: Long,
val coAuthorId: Option[Long]) extends KeyedEntity[Long]
{
def book_id = id
}

object Library extends Schema {
override def name = Some("test_schema")
val authors = table[Author]
val books = table[Book]
}

Maxime Lévesque

unread,
Jun 6, 2011, 10:50:08 AM6/6/11
to squ...@googlegroups.com

Your approach valid, you could also do this

class Author(
     val auth_id: Long,

     var firstName: String,
     var lastName: String,
     var email: Option[String]) extends KeyedEntity[Long]
{
 def id = auth_id
}

on(yourTable)(e => declare(
 e.auth_id      is(primaryKey)
))

Byron Weber Becker

unread,
Jun 6, 2011, 11:13:19 AM6/6/11
to squ...@googlegroups.com
Maxime --

Thanks for your help. Your suggested alternative is working for me except for doing an update. If I say

var jrr = authors.where(a => a.auth_id === 1).single
jrr.firstName = "J.R.R."
authors.update(jrr)

the last line generates the following exception:

java.lang.ClassCastException: java.lang.Long cannot be cast to org.squeryl.dsl.CompositeKey
at org.squeryl.internals.DatabaseAdapter$$anonfun$writeUpdate$6$$anonfun$6.apply(DatabaseAdapter.scala:519)
at org.squeryl.internals.DatabaseAdapter$$anonfun$writeUpdate$6$$anonfun$6.apply(DatabaseAdapter.scala:518)
at org.squeryl.internals.Utils$DummyQuery4WhereClause$$anonfun$$init$$2$$anonfun$apply$3.apply(Utils.scala:71)
at org.squeryl.internals.Utils$DummyQuery4WhereClause$$anonfun$$init$$2$$anonfun$apply$3.apply(Utils.scala:71)
at org.squeryl.dsl.fsm.BaseQueryYield$$anonfun$whereClause$1.apply(BaseQueryYield.scala:61)
at org.squeryl.dsl.fsm.BaseQueryYield$$anonfun$whereClause$1.apply(BaseQueryYield.scala:61)
at scala.Option.map(Option.scala:129)
at org.squeryl.dsl.fsm.BaseQueryYield.whereClause(BaseQueryYield.scala:61)
at org.squeryl.dsl.fsm.BaseQueryYield.queryElements(BaseQueryYield.scala:69)
at org.squeryl.dsl.ast.QueryExpressionNode.<init>(QueryExpressionNode.scala:41)
at org.squeryl.dsl.AbstractQuery.buildAst(AbstractQuery.scala:99)
at org.squeryl.dsl.boilerplate.Query1.<init>(Query1.scala:34)
at org.squeryl.internals.Utils$DummyQuery4WhereClause.<init>(Utils.scala:68)
at org.squeryl.internals.Utils$.createQuery4WhereClause(Utils.scala:76)
at org.squeryl.internals.DatabaseAdapter$$anonfun$writeUpdate$6.apply(DatabaseAdapter.scala:518)
at org.squeryl.internals.DatabaseAdapter$$anonfun$writeUpdate$6.apply(DatabaseAdapter.scala:517)
at scala.Either.fold(Either.scala:42)
at org.squeryl.internals.DatabaseAdapter$class.writeUpdate(DatabaseAdapter.scala:515)
at org.squeryl.adapters.PostgreSqlAdapter.writeUpdate(PostgreSqlAdapter.scala:24)
at org.squeryl.Table._update(Table.scala:161)
at org.squeryl.Table.update(Table.scala:149)
at code.Main$$anonfun$main$1.apply$mcV$sp(Main.scala:53)
at code.Main$$anonfun$main$1.apply(Main.scala:27)
at code.Main$$anonfun$main$1.apply(Main.scala:27)

The exception occurs in the function writeUpdate. It certainly appears to be constructing the
update query that will be issued to the database. Line 519, where the exception occurs, is the
**'d line in the following. There's still a lot I don't know about Scala, but it appears there
is a built-in assumption that the primary key is a composite.

t.posoMetaData.primaryKey.getOrElse(error("writeUpdate was called on an object that does not extend from KeyedEntity[]")).fold(
pkMd => sw.write(quoteName(pkMd.columnName), " = ", writeValue(o_, pkMd, sw)),
pkGetter => {
val astOfQuery4WhereClause = Utils.createQuery4WhereClause(t, (t0:T) =>
** pkGetter.invoke(t0).asInstanceOf[CompositeKey].buildEquality(o.asInstanceOf[KeyedEntity[CompositeKey]].id))

astOfQuery4WhereClause.inhibitAliasOnSelectElementReference = true
astOfQuery4WhereClause.whereClause.get.write(sw)
}
)


I'm using Postgresql v9.

Byron

---------------------------------------------------------
Byron Weber Becker Voice: 519-888-4567 x34661
School of Computer Science Fax: 519-885-1208
University of Waterloo Office: DC3105
Waterloo, ON N2L 3G1

Advising FAQ: http://www.cs.uwaterloo.ca/current/faq/index.shtml

Maxime Lévesque

unread,
Jun 6, 2011, 11:49:28 AM6/6/11
to squ...@googlegroups.com

This is a bug, you'll have to use the other solution, meanwhile I'm treating this as a hight priority bug.

http://www.assembla.com/spaces/squeryl/tickets/55-keyedentity-with-def-id-refering-to-another-field-incorrectly-assumes-a-composite-key

ML

ptw

unread,
Mar 26, 2012, 3:24:32 PM3/26/12
to squ...@googlegroups.com
Still not working in 0.9.5-RC1?

David Whittaker

unread,
Mar 26, 2012, 4:08:20 PM3/26/12
to squ...@googlegroups.com
The bug still appears to be open, but in my opinion, the best way to handle this is with the original suggestion.  Add a @Column(name = "myId") attribute to the id member and then add an alias to it with def myId = id.

BTW, the latest in the 0.9.5 series is Squeryl-0.9.5-RC2 which is available on Maven Central.

-Dave

On Mon, Mar 26, 2012 at 3:24 PM, ptw <ptw...@gmail.com> wrote:
Still not working in 0.9.5-RC1?

artiee

unread,
May 10, 2012, 7:27:36 AM5/10/12
to squ...@googlegroups.com
Hi,

Also ran into this bug. The solution works, but annotations in my code don't look very nice. Hope this gets fixed soon.

Br's ,

Artiee
Reply all
Reply to author
Forward
0 new messages