Scala Slick delete not working

2019-08-25 04:06发布

问题:

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.