【发布时间】:2017-03-08 23:53:00
【问题描述】:
我正在尝试使用 Slick 将一些数据插入到我的数据库中。我已经成功地能够查询数据库,但不太明白如何使用文档示例插入数据。
我已经到了一个错误的地步,即我的action 的类型不正确,它会引发错误
type mismatch;
found : slick.dbio.DBIOAction[Unit,slick.dbio.NoStream,slick.dbio.Effect.Write with slick.dbio.Effect.Schema]
required: slick.dbio.DBIOAction[com.ojolabs.customer.avro.CustomerEvent,slick.dbio.NoStream,Nothing]
db.run(action)
我不太确定如何使用我已经编写的代码返回指定的类型。
我从这里调用我的架构:
trait CustomerEventsComponent {
def customEventsManager: CustomerEvents.Async
}
trait DefaultCustomerEvents extends CustomerEventsComponent{
self: DatabaseComponent with ExecutionContextComponent =>
lazy val customEventsManager = new Async {
override def create(phoneNumber: String, createdAt: DateTime): Future[CustomerEvent] = {
val action = Schema.CustomerEvents.userAction
//this is the line that throws the error
db.run(action)
}
}
}
我在这里创建动作
object Schema {
class CustomerEvents(tag: Tag) extends Table[CustomerEvent](tag, "customer_events") {
def id: Rep[UUID] = column[UUID]("id", O.PrimaryKey)
def customerId: Rep[UUID] = column[UUID]("customer_id")
def eventType: Rep[String] = column[String]("type")
def createdAt: Rep[DateTime] = column[DateTime]("created_at")
def version: Rep[Double] = column[Double]("version")
def data: Rep[JsValue] = column[JsValue]("data")
def metadata: Rep[JsValue] = column[JsValue]("metadata")
def * = (id, customerId, eventType, createdAt, version, data, metadata) <> (CustomerEvent.tupled, CustomerEvent.unapply)
}
object CustomerEvents {
val all = TableQuery[CustomerEvents]
val userAction = DBIO.seq(
all.schema.create,
all += CustomerEvent(
UUID.randomUUID(),
UUID.randomUUID(),
"hello",
DateTime.now(),
1.0,
Json.toJson("{\"hello\" : \"its me\"}"),
Json.toJson("{\"hello\" : \"its me\"}"))
)
}
【问题讨论】: