Scala Slick - 省略 ID 列 (Auto_Increment)
Scala Slick - Omit ID column (Auto_Increment)
学了2个月的Scala,Slick,Akka,在做Akka的项目时遇到了问题...
// This case class is to use for parsing data from request
// case class UserTransaction(sender: String, recipient: String, amount: Int)
//This one is to use for reflecting database
case class UserTransactionDB(sender: String, recipient: String, amount: Int, id: Int)
class UserTransactionModelDB(tag: Tag) extends Table[UserTransactionDB](tag, "usertransaction")
{
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def sender = column[String]("sender")
def recipient = column[String]("recipient")
def amount = column[Int]("amount")
override def * =
(sender, recipient, amount, id) <> (UserTransactionDB.tupled, UserTransactionDB.unapply)
}
我想像这样向 Akka 发送 POST 请求 (Json) :
{"sender" : "S" , "recipient" : "R", "amount" : 100}
现在,我只想使用一种情况 class UserTransaction(没有 UserTransactionDB 中的 "id" 字段),不仅要反映数据库,还要解析请求中的数据。这可能吗?
谢谢你,对不起我的英语!
尝试使用自定义格式...大致如下:
case class UserTransactionDB(sender: String, recipient: String, amount: Int, id: Int)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit object UserTransactionJsonFormat extends RootJsonFormat[UserTransactionDB] {
def write(c: UserTransactionDB) =
JsArray(JsString(c.sender), JsString(c.recipient), JsNumber(c.amount))
def read(value: JsValue) = value match {
case JsArray(Vector(JsString(sender), JsString(recipient), JsNumber(amount))) =>
new UserTransactionDB(sender, recipient, amount.toInt)
case _ => deserializationError("UserTransactionDB expected")
}
}
}
学了2个月的Scala,Slick,Akka,在做Akka的项目时遇到了问题...
// This case class is to use for parsing data from request
// case class UserTransaction(sender: String, recipient: String, amount: Int)
//This one is to use for reflecting database
case class UserTransactionDB(sender: String, recipient: String, amount: Int, id: Int)
class UserTransactionModelDB(tag: Tag) extends Table[UserTransactionDB](tag, "usertransaction")
{
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def sender = column[String]("sender")
def recipient = column[String]("recipient")
def amount = column[Int]("amount")
override def * =
(sender, recipient, amount, id) <> (UserTransactionDB.tupled, UserTransactionDB.unapply)
}
我想像这样向 Akka 发送 POST 请求 (Json) :
{"sender" : "S" , "recipient" : "R", "amount" : 100}
现在,我只想使用一种情况 class UserTransaction(没有 UserTransactionDB 中的 "id" 字段),不仅要反映数据库,还要解析请求中的数据。这可能吗?
谢谢你,对不起我的英语!
尝试使用自定义格式...大致如下:
case class UserTransactionDB(sender: String, recipient: String, amount: Int, id: Int)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit object UserTransactionJsonFormat extends RootJsonFormat[UserTransactionDB] {
def write(c: UserTransactionDB) =
JsArray(JsString(c.sender), JsString(c.recipient), JsNumber(c.amount))
def read(value: JsValue) = value match {
case JsArray(Vector(JsString(sender), JsString(recipient), JsNumber(amount))) =>
new UserTransactionDB(sender, recipient, amount.toInt)
case _ => deserializationError("UserTransactionDB expected")
}
}
}