2013-12-26 1 views
1

Я хочу сохранить Rescource с ForeignKey Пользователейкак сделать Option [Int] сохранить в Скале пятно

case class User(id: Option[Int], name : String) 

object Users extends Table[User]("users") { 
def id = column[Int]("id", O.PrimaryKey) 
def name = column[String]("name", O.NotNull) 
def * = id.? ~ name <> (User, User.unapply _) 

def add(user: User)(implicit session: Session) = { 
    this.insert(user) 
} 

def countByName(name: String)(implicit session: Session) = { 
    (for { 
    user <- Users 
    if (user.name === name) 
    } yield(user)).list.size 
} 

} 
case class Resource(id: Option[Long] = None, owner: Int, types: String) 


object Resources extends Table[Resource]("RESOURCE") { 
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc) 
def owner = column[Int]("Owner") 
def types = column[String]("Type") 
def withuser = foreignKey("User_FK", owner, Users)(_.id) 

// Every table needs a * projection with the same type as the table's type parameter 
def * = id ~ owner ~ types <> (Resource, Resource.unapply _) 
} 

при компиляции ошибка:

[error] F:\mysource\play-slick\app\models\Resource.scala:17: overloaded method v 
alue <> with alternatives: 
[error] [R(in method <>)(in method <>)(in method <>)(in method <>)(in method < 
>)(in method <>)(in method <>)(in method <>)](f: (Long, Int, String) => R(in met 
hod <>)(in method <>)(in method <>)(in method <>)(in method <>)(in method <>)(in 
method <>)(in method <>), g: R(in method <>)(in method <>)(in method <>)(in met 
hod <>)(in method <>)(in method <>)(in method <>)(in method <>) => Option[(Long, 
Int, String)])scala.slick.lifted.MappedProjection[R(in method <>)(in method <>) 
(in method <>)(in method <>)(in method <>)(in method <>)(in method <>)(in method 
<>),(Long, Int, String)] <and> 
[error] [R(in method <>)(in method <>)(in method <>)(in method <>)(in method < 
>)(in method <>)(in method <>)(in method <>)](f: ((Long, Int, String)) => R(in m 
ethod <>)(in method <>)(in method <>)(in method <>)(in method <>)(in method <>)(
in method <>)(in method <>), g: R(in method <>)(in method <>)(in method <>)(in m 
ethod <>)(in method <>)(in method <>)(in method <>)(in method <>) => Option[(Lon 
g, Int, String)])scala.slick.lifted.MappedProjection[R(in method <>)(in method < 
>)(in method <>)(in method <>)(in method <>)(in method <>)(in method <>)(in meth 
od <>),(Long, Int, String)] 
[error] cannot be applied to (models.Resource.type, models.Resource => Option[(
Option[Long], Int, String)]) 
[error] def * = id ~ owner ~ types <> (Resource, Resource.unapply _) 
[error]       ^

Я думаю, что это проблема серийного варианта [Int], любой знает, как сохранить опцию [Int]

ответ

1

Ваш класс Resource не соответствует вашему объекту Resources. У вас есть Int, String, String, совпадающий со спутником, который имеет Long, Int, String. Изменение Resources на:

object Resources extends Table[Resource]("RESOURCE") { 
    def id = column[Long]("ID", O.PrimaryKey, O.AutoInc) 
    def owner = column[String]("Owner") 
    def types = column[String]("Type") 

    def withuser = foreignKey("User_FK", owner, Users)(_.id) 
} 

Кроме того, я рекомендую изменять все свои значения идентификатора класса Option[Int] случая к Option[Long], в соответствии с вашими объектами.

UPDATE:

Как уже упоминалось пользователь @cvogt, вы также должны .? на дополнительных полей в вашей проекции, как следующее:

def * = id.? ~ owner ~ types <> (Resource, Resource.unapply _) 
+0

Я изменил ресурс для случая ресурсов класса (id: Option [Long] = None, владелец: Int, types: String) ошибка все еще существует, почему? – user504909

+0

добавить '.?' В id. Я изменил ответ – cvogt

Смежные вопросы