Noob: how do postgre AutoInc with slick 2.1

89 views
Skip to first unread message

RyM

unread,
Jan 11, 2015, 6:48:01 PM1/11/15
to scala...@googlegroups.com
Hi guys, sorry for the super noob question but I could found info about how use autoinc with slick 1 but not with slick 2.1

I try this

class Buyers(tag: Tag) extends Table[(Option[Long], String, Int, String)](tag, "buyers") {
  def id  = column[Option[Long]]("id",O.PrimaryKey,O.AutoInc)
  def name = column[String]("name")
  def city     = column[String]("city")
  def * = (id.?, name, city)
}



based in the sample


case
class User(id: Option[Int], first: String, last: String) class Users(tag: Tag) extends Table[User](tag, "users") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def first = column[String]("first") def last = column[String]("last") def * = (id.?, first, last) <> (User.tupled, User.unapply) }

but I get error
<console>:19: error: 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: scala.slick.lifted.FlatShapeLevel
     Source type: (scala.slick.lifted.Column[Option[Long]], scala.slick.lifted.Column[String], scala.slick.lifted.Column[String])
   Unpacked type: (Option[Long], String, Int, String)
     Packed type: Any

         def * = (id, name, city)


I also try


case
class Supplier(id: Int, name: String, street: String) class Suppliers(tag: Tag) extends Table[Supplier](tag, "SUPPLIERS") { def id = column[Int]("SUP_ID", O.PrimaryKey, O.AutoInc) def name = column[String]("SUP_NAME") def street = column[String]("STREET") def * = (id, name, street) <> (Supplier.tupled, Supplier.unapply) } val suppliers = TableQuery[Suppliers] suppliers.insert(mySupplier)


but is not clear how add new suppliers in this way...
suppliers.insert(Supplier(???? , "john smith","212"))   if I supply an Int as id I suppose than the autoinc is not applied, don't must be the id an option type??...

sim

unread,
Jan 12, 2015, 11:44:37 AM1/12/15
to scala...@googlegroups.com



suppliers.insert(Supplier(0 , "john smith","212")) or suppliers.map(s = > (s.name,s.street)) += ("john smith","212")

both will work


 

Gabor Pihaj

unread,
Aug 15, 2015, 12:06:36 PM8/15/15
to Slick / ScalaQuery
Hi,

I have the same problem with Slick 3.0.1.
My code looks almost the same: 

  case class User(
    id: Option[Long],
    firstName: String,
    lastName: String
  )

  class UserTable(tag: Tag) extends Table[User](tag, "user") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def firstName = column[String]("first_name")
    def lastName = column[String]("last_name")

    def * = (id.?, firstName, lastName) <> (User.tupled, User.unapply)
  }

  val users = TableQuery[UserTable]
 
  val query = for(record <- users) yield record.*
  db.run(query.result) onComplete {
    case Success(users) => println(users)
    case Failure(t)     => println(t)
  }

And I get this error:

 No matching Shape found.
[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
[error]   Required level: slick.lifted.FlatShapeLevel
[error]      Source type: slick.lifted.ProvenShape[com.brickstats.ar.UserModel.User]
[error]    Unpacked type: T
[error]      Packed type: G
[error]     val query = for(record <- users) yield record.*
[error]                             ^


What Am I missing?

Thanks,
Gabor

Gabor Pihaj

unread,
Aug 15, 2015, 5:27:48 PM8/15/15
to Slick / ScalaQuery
In my case the error was in the approach to get all users.

This exactly what I wanted to do:

val query = users
val usersList = Await.result(db run query.result, 1.seconds)

Reply all
Reply to author
Forward
0 new messages