Autoinsert UUID on object creation

72 views
Skip to first unread message

Alexander Zafirov

unread,
Aug 10, 2015, 5:22:23 AM8/10/15
to Slick / ScalaQuery
I have an object with a field that's of type UUID:

def uuid = column[UUID]("uuid", O.PrimaryKey)

I would like hide from the client the possibility to enter uuids but rather provide them when the object is instantiated. I figured that I need to have a

case class Foo private(uuid, ...)

and then create a companion object with an apply method that would generate UUID.randomUUID().

The problem is that once I create the apply method I get two errors:
- "method unapply is defined twice"
- "ambiguous reference to overloaded definition, both method apply [...] match expected type ?"

I found this but it wasn't very useful. 

Any help would be appreciated,

Alex




Mirco Dotta

unread,
Aug 10, 2015, 7:49:29 AM8/10/15
to scala...@googlegroups.com
This is a known limitation of case classes in Scala. You can work around it by declaring the case class abstract.

Cheers,
Mirco
----------------
Mirco Dotta - @mircodotta

Typesafe – Build reactive apps!

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/f56e3434-1be6-4978-85ea-2fbd9eda4536%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Alexander Zafirov

unread,
Aug 10, 2015, 9:22:19 AM8/10/15
to Slick / ScalaQuery
Hi Mirco,

Can you please elaborate a bit more on how I instantiate the case class in the apply method of the companion object once it is declared as abstract?

Best,
Alex

Mirco Dotta

unread,
Aug 10, 2015, 10:36:25 AM8/10/15
to scala...@googlegroups.com
Hi Alex, sure thing. Here you go

abstract case class A private(a: Int)

object A {
  def apply(): A = new A(2){}
}
----------------
Mirco Dotta - @mircodotta

Typesafe – Build reactive apps!

signature.asc

Alexander Zafirov

unread,
Aug 26, 2015, 8:10:05 AM8/26/15
to Slick / ScalaQuery
Hi Mirco,

And what about the mapping of the class?

For example how do I resolve this case:

abstract case class User(foo: Int, name: String, id: Option[Int] = None)

object User {
  def apply(name: String): User = new User(1, name){}
}
class Users(tag: Tag) extends Table[User](tag, "USERS") {
  // Auto Increment the id primary key column
  def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)

  // The name can't be null
  def name = column[String]("NAME")

  // the * projection (e.g. select * ...) auto-transforms the tupled
  // column values to / from a User
  def * = (name, id.?) <> (User.apply, User.unapply)
}


This gives me 

Error:(58, 33) type mismatch;
 found   : String => User
 required: ((Int, String, Option[Int])) => User
  def * = (name, id.?) <> (User.apply, User.unapply)
                                ^
and 
Error:(58, 24) No matching Shape found.
Slick does not know how to map the given types.
Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
  Required level: slick.lifted.FlatShapeLevel
     Source type: (slick.lifted.Rep[String], slick.lifted.Rep[Option[Int]])
   Unpacked type: (Int, String, Option[Int])
     Packed type: Any
  def * = (name, id.?) <> (User.apply, User.unapply)
                       ^

Mirco Dotta

unread,
Sep 15, 2015, 7:31:06 AM9/15/15
to scala...@googlegroups.com
I think you’ll have to write a mapping function. See http://slick.typesafe.com/doc/3.0.0/schemas.html#mapped-tables for some context.

----------------
Mirco Dotta - @mircodotta

Typesafe – Build reactive apps!

signature.asc
Reply all
Reply to author
Forward
0 new messages