如何使用Slick2.0.1映射postgresql自定义枚举列? [英] How to map postgresql custom enum column with Slick2.0.1?

查看:44
本文介绍了如何使用Slick2.0.1映射postgresql自定义枚举列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想不通.我现在正在使用的是:

I just can't figure it out. What I am using right now is:

abstract class DBEnumString extends Enumeration {
  implicit val enumMapper = MappedJdbcType.base[Value, String](
     _.toString(),
     s => this.withName(s)
  )
}

然后:

object SomeEnum extends DBEnumString {
  type T = Value
  val A1 = Value("A1")
  val A2 = Value("A2")
}

问题是,在PostgreSQL的插入/更新JDBC驱动程序期间,当列类型为"some_enum"时,参数类型会因字符变化"而抱怨,这在我将SomeEnum转换为String时是合理的.

The problem is, during insert/update JDBC driver for PostgreSQL complains about parameter type being "character varying" when column type is "some_enum", which is reasonable as I am converting SomeEnum to String.

如何告诉Slick将String视为DB定义的"enum_type"?还是如何定义其他一些映射到"enum_type"的Scala类型?

How do I tell Slick to treat String as DB-defined "enum_type"? Or how to define some other Scala-type that will map to "enum_type"?

推荐答案

当我尝试让我的postgreSQL枚举与光滑代码一起使用时,我也有类似的困惑. Slick-pg 允许您将Scala枚举与数据库枚举一起使用,并且测试套件显示如何

I had similar confusion when trying to get my postgreSQL enums to work with slick. Slick-pg allows you to use Scala enums with your databases enums, and the test suite shows how.

下面是一个例子.

说我们的数据库中有这种枚举类型.

Say we have this enumerated type in our database.

CREATE TYPE Dog AS ENUM ('Poodle', 'Labrador');

我们希望能够将它们映射到Scala枚举,因此我们可以与Slick一起愉快地使用它们.我们可以使用slick-pg(slick的扩展名)来做到这一点.

We want to be able to map these to Scala enums, so we can use them happily with Slick. We can do this with slick-pg, an extension for slick.

首先,我们为上述枚举创建一个Scala版本.

First off, we make a Scala version of the above enum.

object Dogs extends Enumeration {
  type Dog = Value
  val Poodle, Labrador = Value
}

要从slick-pg获得更多功能,我们扩展了普通的PostgresDriver,并说我们想将Scala枚举映射到PostgreSQL一个(记住,将application.conf中的slick驱动程序更改为您创建的那个).

To get the extra functionality from slick-pg we extend the normal PostgresDriver and say we want to map our Scala enum to the PostgreSQL one (remember to change the slick driver in application.conf to the one you've created).

object MyPostgresDriver extends PostgresDriver with PgEnumSupport {
  override val api = new API with MyEnumImplicits {}

  trait MyEnumImplicits {
    implicit val dogTypeMapper = createEnumJdbcType("Dog", Dogs)
    implicit val dogListTypeMapper = createEnumListJdbcType("Dog", Dogs)

    implicit val dogColumnExtensionMethodsBuilder = createEnumColumnExtensionMethodsBuilder(Dogs)
    implicit val dogOptionColumnExtensionMethodsBuilder = createEnumOptionColumnExtensionMethodsBuilder(Dogs)
  }
}

现在,当您要创建新的模型案例类时,只需使用相应的Scala枚举即可.

Now when you want to make a new model case class, simply use the corresponding Scala enum.

case class User(favouriteDog: Dog) 

当您完成整个DAO表的恶作剧时,您可以再次使用它.

And when you do the whole DAO table shenanigans, again you can just use it.

class Users(tag: Tag) extends Table[User](tag, "User") {
  def favouriteDog = column[Dog]("favouriteDog")
  def * = (favouriteDog) <> (Dog.tupled, Dog.unapply _)
}

很明显,无论使用在哪里,都需要Scala Dog枚举.

Obviously you need the Scala Dog enum in scope wherever you use it.

由于错误的出现,当前您无法动态链接到 application.conf 中的自定义滑动驱动程序(应该可以使用).这意味着您要么需要从头开始运行play框架而不进行动态重新编译,要么可以创建一个仅包含自定义滑动驱动程序的独立sbt项目,并在本地依赖该项目.

Due to a bug in slick, currently you can't dynamically link to a custom slick driver in application.conf (it should work). This means you either need to run play framework with start and not get dynamic recompiling, or you can create a standalone sbt project with just the custom slick driver in it and depend on it locally.

这篇关于如何使用Slick2.0.1映射postgresql自定义枚举列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆