【问题标题】:Scala Slick: Type Mismatch "String vs Tables.Row" during insertScala Slick:插入期间类型不匹配“String vs Tables.Row”
【发布时间】:2021-11-19 18:01:24
【问题描述】:

Schema 是从 PostgreSQl 数据库自动生成的:

class Geopointtable(_tableTag: Tag) extends profile.api.Table[GeopointtableRow](_tableTag, "geopointtable") {
   def * = (id, latitude, longitude, date, time) <> (GeopointtableRow.tupled, GeopointtableRow.unapply)

   def ? = ((Rep.Some(id), Rep.Some(latitude), Rep.Some(longitude), Rep.Some(date), Rep.Some(time))).shaped.<>({r=>import r._; _1.map(_=> GeopointtableRow.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))

   val id: Rep[Int] = column[Int]("id", O.AutoInc, O.PrimaryKey)
   val latitude: Rep[Int] = column[Int]("latitude")
   val longitude: Rep[Int] = column[Int]("longitude")
   val date: Rep[java.sql.Date] = column[java.sql.Date]("date")
   val time: Rep[java.sql.Time] = column[java.sql.Time]("time")
 }
   lazy val Geopointtable = new TableQuery(tag => new Geopointtable(tag))
}

我正在尝试在表中插入一些值:

val db = Database.forConfig("geodb")
val date = java.sql.Date.valueOf(LocalDateTime.now().toLocalDate.toString)
val time = java.sql.Time(LocalDateTime.now().toLocalTime)
var point = TableQuery[Tables.Geopointtable]

Await.result(
  db.run(
  point += Tables.GeopointtableRow(1, 2, 3, date, time))
, Duration.Inf)

但对于“Tables.GeopointtableRow(1, 2, 3, date, time)) 行,编译器指定 2 种类型不匹配:

  1. 必需的字符串,找到 Tables.GeopointtableRow"。
  2. 必需的 DBIOAction[NotInferedR,NoStream,Nothing],找到单位

【问题讨论】:

    标签: scala slick


    【解决方案1】:

    您需要告诉 Slick 要将记录插入到哪个表中。你可能想要类似的东西

    db.run(
      Tables.Geopointtable += Tables.GeopointtableRow(1, 2, 3, date, time)
    )
    

    【讨论】:

    • 我很早就试过了。但结果相同。
    • 蒂姆,我更新了问题,添加了更多不匹配的信息
    • 将操作放在单独的行上,因此有两个单独的错误:val action = Tables.Geopointtable += Tables.GeopointtableRow(...) 然后db.run(action)
    • 我做到了。现在第一个不匹配(与字符串)出现在 val action = ... 表达式中。 db.run(action) 的第二个不匹配(与 Unit)
    【解决方案2】:

    问题解决了。

    我只在我的类中导入以下依赖项:“Tables.”和“Tables.profile.api.”。

    所以现在我不是通过“Tables.Geopointtable”而是直接应用到生成的表格 - “Geopointtable”。

    我想知道,为什么我的 IDEA 不建议添加此导入,这是有原因的。 ))

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 1970-01-01
      • 2015-12-03
      • 2016-06-14
      • 2015-04-03
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多