How to insert value of UUID? How to insert value of UUID? postgresql postgresql

How to insert value of UUID?


Anorm is DB agnostic, as JDBC, so vendor specific datatype are not supported by default.

You can use {id}::uuid in the statement, so that the java.util.UUID passed as String in JDBC parameters is then converted from passed VARCHAR to a specific PostgreSQL uuid.

Using string interpolation in SQL(s"...") is not recommanded (SQL injection), but Anorm interpolation can be used.

def insert(email:EmailQueue): Unit = DB.withTransaction { implicit c =>  SQL"""    INSERT INTO "email_queue" ( "body", "created_date", "id", "is_sent", "send_from", "send_to", "subject", "email_template")    VALUES ( ${email.body}, ${email.created_date}, ${email.id}::uuid, ${email.is_sent}, ${email.send_from}, ${email.send_to}, ${email.subject}, ${email.email_template} )  """).executeInsert()}

Not recommended, but can be useful sometimes for vendor specific type, the anorm.Object can be used to pass an opaque value as JDBC parameter (there the ::uuid is nicer for me).

You can also implement a custom ToStatement[java.util.UUID].