Scala Slick delete not working

一世执手 提交于 2019-12-25 05:49:08

问题


when deleting from my tablequery object I should be able to write:

FacebookAuths.delete

But it's complaining that delete isn't a method in TableQuery, even if I try:

Users.filter(_.id === 1337).delete

It's still saying that delete isn't a method, but now on the Query object.

What am I doing wrong? My imports are:

import scala.slick.lifted._
import scala.slick.driver.JdbcDriver.simple._

And all other things like firstOption works.

I use postgres.

Thanks!


回答1:


You are using Postgres, so you need to import scala.slick.driver.PostgresDriver.simple._ and scala.slick.driver.PostgresDriver instead of the jdbc ones, the same thing applies to where your schema is defined.

Edit:

This is a bit outside my knowledge and I'm not 100% sure but I will give it a try.

The PostgresDriver trait extends the JdbcDriver trait (from JdbcProfile.scala), this is the trait signature:

trait PostgresDriver extends JdbcDrive

and in turn JdbcDriver extends SqlDriver:

trait JdbcDriver extends SqlDriver

The firstOption method belongs to the UnitInvoker trait, so it's not dependent from the imported drivers, the same thing applies to list and first and other methods, you can check them in Invoker.scala file. The delete method instead is defined in the DeleteInvoker class inside the JdbcInvokerComponent trait.

My understanding is that when declaring a TableQuery object this is the full signature:

val table: PostgresDriver.simple.TableQuery[MyTable] = TableQuery[MyTable]

While you are declaring a table with this signature:

val table: JdbcDriver.simple.TableQuery[MyTable] = TableQuery[MyTable]

I don't know why the delete method is not available for Jdbc directly, probably you have to use Query for that and then use Query.deleteInvoker, but as I said I'm not sure, it looks confusing to me too.



来源:https://stackoverflow.com/questions/24000759/scala-slick-delete-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!