简体   繁体   English

光滑:比较Rep [Option [Blob]]与Rep [Int]

[英]Slick: comparing Rep[Option[Blob]] with Rep[Int]

I am using Slick to analyze a legacy MySQL database (with MyISAM engine). 我正在使用Slick分析旧版MySQL数据库(使用MyISAM引擎)。 And I'm using implicit classes to navigate through entities, eg user.logs with this code: 我正在使用隐式类浏览实体,例如,使用以下代码的user.logs

implicit class UserNav(user: User) {
    def logs = Logs.filter(_.userId === user.id)
}

However, in this case the key is an INT but the foreign key is a BLOB. 但是,在这种情况下,键是INT,而外键是BLOB。 Using a MySQL client I can run select userId * 1 from logs to get an INT from the BLOB, and I can even join despite the different datatypes. 使用MySQL客户端,我可以select userId * 1 from logs运行select userId * 1 from logs以从BLOB获取INT,尽管数据类型不同,我什至可以加入。 But with Slick I get a compile errors for the code above. 但是使用Slick,我得到了上面代码的编译错误。

error: Cannot perform option-mapped operation [ERROR]
with type: (Option[java.sql.Blob], Int) => R [ERROR]   
for base type:(java.sql.Blob, java.sql.Blob) => Boolean

error: ambiguous implicit values:
both value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[slick.lifted.Rep[Option[Boolean]]]
and value BooleanCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[Boolean]
match expected type slick.lifted.CanBeQueryCondition[Nothing]

Any idea how to solve this? 任何想法如何解决这个问题?

As I found out by trying, just pretending the column has a different data type works. 正如我通过尝试发现的那样,只是假装该列具有不同的数据类型。

So besides the original val in class Log (which extends Table[LogRow] ) 因此,除了类Log的原始val (它扩展了Table[LogRow] )之外,

val userId: Rep[Option[java.sql.Blob]] = column[Option[java.sql.Blob]]("userId", O.Default(None))

I added 我加了

val userId_asInt: Rep[Option[Int]] = column[Option[Int]]("userId", O.Default(None))

and changed the navigation def to 并改变了导航def

def logs = Logs.filter(_.userId_asInt === user.id)

This did the trick – at least with MySQL/MyISAM (no idea if this works with other engines or even other DBMS). 这确实成功了–至少与MySQL / MyISAM结合使用(不知道是否与其他引擎甚至其他DBMS一起使用)。

Instead I could also have replaced the data type in original val userId , but then I would also have to change that data type all over the place like in Log.* and Log.? 相反,我也可以替换原始val userId的数据类型,但是随后我还必须像在Log.*Log.?那样在各处更改该数据类型Log.? ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM