How to map postgresql custom enum column with Slick2.0.1?

1,000 views
Skip to first unread message

Marcin Kowalski

unread,
Apr 9, 2014, 4:06:23 AM4/9/14
to scala...@googlegroups.com
Hi,

I am using postgresql with Slick 2.01 and I have a table with custom enum type column. How do I make Slick translate its Scala type to pg-type properly on insert/update? Is it possible? What I am doing right now is I have scala enum and translate it to string on insert/update. The problem is JDBC driver complains about type incompatibility for query parameters. It expects enums, but gets a string. Please take a look at http://stackoverflow.com/questions/22945485/how-to-map-postgresql-custom-enum-column-with-slick2-0-1

Thank you,

Christopher Vogt

unread,
Apr 9, 2014, 8:17:15 AM4/9/14
to scala...@googlegroups.com
Please provide reproduce code and and the exact error message.
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Slick / ScalaQuery" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scalaquery+...@googlegroups.com
> <mailto:scalaquery+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/scalaquery/fe61e9f7-5ddd-429b-9909-a3645ff63f90%40googlegroups.com
> <https://groups.google.com/d/msgid/scalaquery/fe61e9f7-5ddd-429b-9909-a3645ff63f90%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Marcin Kowalski

unread,
Apr 11, 2014, 10:38:13 AM4/11/14
to scala...@googlegroups.com
I will not post the exact error message, because it is unfortunately in Polish (my postgresql has Polish locale). Anyway, as close as I can get in English is "column "status" is of type enum_status but expression is of type character varying". I believe that what postgresql (or jdbc driver) wants is cast from character varying to db enum type, like "CAST(xxx AS some_enum_type)" but I just can find no way to force Slick to add it to generated insert/update statements. I do not want to use plain sql queries nor implicit postgresql casts.

The problem with code below is it converts scala enum object to string, which is mapped into character varying instead of enum which is not what postgresql expects. Any help appreciated.

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

//Actual enum object
object EntryStatus extends DBEnumString {
  type T = Value
  val A1 = Value("A1")
  val A2 = Value("A2")
}


//Slick table definition
class UserEntries(tag: Tag) extends Table[(Int, Int, EntryStatus.T)](tag, "user_entries") { def userId = column[Int]("user_id") def entryId = column[Int]("entry_id") def status = column[EntryStatus.T]("status") def * = (userId, entryId, status) }

val userEntries = TableQuery[UserEntries]

//Dao method - this is where exception is thrown
def addOrUpdateUserEntry(userId: Int, entryId: Int, entryStatus: EntryStatus.T)(implicit s: Session) = { if (!userEntries.where(ue => { ue.userId === userId && ue.entryId === entryId }).exists.run) { userEntries += (userId, entryId, entryStatus) } else { userEntries.where(ue => { ue.userId === userId && ue.entryId === entryId }).map(_.status).update(entryStatus) } }

On Wednesday, 9 April 2014 14:17:15 UTC+2, Christopher Vogt wrote:
Please provide reproduce code and and the exact error message.

On 09.04.14 10:06, Marcin Kowalski wrote:
> Hi,
>
> I am using postgresql with Slick 2.01 and I have a table with custom
> enum type column. How do I make Slick translate its Scala type to
> pg-type properly on insert/update? Is it possible? What I am doing right
> now is I have scala enum and translate it to string on insert/update.
> The problem is JDBC driver complains about type incompatibility for
> query parameters. It expects enums, but gets a string. Please take a
> look at
> http://stackoverflow.com/questions/22945485/how-to-map-postgresql-custom-enum-column-with-slick2-0-1
>
> Thank you,
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Slick / ScalaQuery" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scalaquery+...@googlegroups.com

Christopher Vogt

unread,
Apr 11, 2014, 6:13:40 PM4/11/14
to scala...@googlegroups.com
Try setting the type to ENUM using a ColumnOption like this:

> def status = column[EntryStatus.T]("status",O.DBType("ENUM"))

Does that help?

Chris

Marcin Kowalski

unread,
Apr 16, 2014, 4:38:13 AM4/16/14
to scala...@googlegroups.com
It does not. Afair DBType is only used in DDL

Marcin Kowalski

unread,
Apr 16, 2014, 5:49:46 AM4/16/14
to scala...@googlegroups.com
I have managed to solve it with slick-pg: https://github.com/tminglei/slick-pg Enum support was added in recent version.

sira...@gmail.com

unread,
Apr 29, 2015, 10:39:40 AM4/29/15
to scala...@googlegroups.com
Hi,

I'm facing the same problem, but could not resolve it with slick-pg.
Could you post the changes of your cod of what you changed compared to the snippet above?
Message has been deleted

christop...@gmail.com

unread,
Dec 10, 2015, 2:30:29 PM12/10/15
to Slick / ScalaQuery
Hi,

I have the same issue. 
Is there a way to use Postgresql enumerated type in a table column and to map it with slick 3.1.0?

Thanks for any suggestion.

Gérald 
Reply all
Reply to author
Forward
0 new messages