Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you insert a Postgres enum value using Clojure JDBC?

Tags:

sql

clojure

jdbc

For example, here is a product table in PostgreSQL with status as an enum:

create type product_status as enum ('InStock', 'OutOfStock');

create table product (
    pid            int primary key default nextval('product_pid_seq'),
    sku            text not null unique,
    name           text not null,
    description    text not null,
    quantity       int not null,
    cost           numeric(10,2) not null,
    price          numeric(10,2) not null,
    weight         numeric(10,2),
    status         product_status not null
);

Typical Clojure code to insert a product would be:

(def prod-12345 {:sku "12345"
                 :name "My Product"
                 :description "yada yada yada"
                 :quantity 100
                 :cost 42.00
                 :price 59.00
                 :weight 0.3
                 :status "InStock"})

(sql/with-connection db-spec
   (sql/insert-record :product prod-12345))

However, status is an enum so you can't insert it as a normal string without casting it to an enum:

'InStock'::product_status

I know you can do it with a prepared statement, such as:

INSERT INTO product (name, status) VALUES (?, ?::product_status)

But is there a way to do it without using a prepared statement?

like image 260
espeed Avatar asked Feb 05 '13 23:02

espeed


People also ask

How do I create an enum in PostgreSQL?

For defining enum data type we need to create the type of enum first to use it into the table, we can define multiple values in enum type to use the same into the table. If we need same column values in table, same time we have using enum. Below is the syntax of enum in PostgreSQL: Create enum type.

Does PostgreSQL have enum?

When using PostgreSQL, each ENUM type is registered in the system catalogs and can be used anywhere PostgreSQL expects a type name. Internally, the ENUM values are stored as integers. It is important to realize that each ENUM type in PostgreSQL is registered in the system catalogs.

Where are enums stored Postgres?

Postgres Enums are created using the CREATE TYPE statement. The values are ordered in the order in which they are specified in the CREATE statement. Postgres stores Enums in the pg_type catalog.

Should you use enum Postgres?

PostgreSQL enums should be used for values that directly impact application control flow. That is, they are for values that have specific meaning to the application code. They are not just data. For example, consider an image conversion system where users can convert images from one format to another.


1 Answers

I got this working today using the stringtype=unspecified hack workaround.

You can add this parameter to your db-spec as follows:

(def db-spec {:classname "org.postgresql.Driver"
              :subprotocol "postgresql"
              :subname "//myserver:5432/mydatabase"
              :user "myuser"
              :password "mypassword"
              :stringtype "unspecified"}) ; HACK to support enums

Then just use insert! as usual.

It would be good to have a solution that doesn't weaken type safety so much.

like image 179
Drew Noakes Avatar answered Sep 20 '22 20:09

Drew Noakes