honeysql-postgres icon indicating copy to clipboard operation
honeysql-postgres copied to clipboard

UNIQUE on the table

Open arichiardi opened this issue 6 years ago • 4 comments

I would like to achieve the following (from here:

CREATE TABLE example (
    a integer,
    b integer,
    c integer,
    UNIQUE (a, c)
);

And I see that there is an (sql/call :unique ...) I can use, but the problem is that I don't really see how to achieve that, maybe because I don't know how to hook in that :with-columns map.

Any recommendation on how to do that?

arichiardi avatar Feb 01 '18 19:02 arichiardi

This is the ugly workaround:

(let [unique-sql & _] (when unique-columns
                        (-> (apply sql/call :unique unique-columns)
                            (sql/format :parameterizer :postgresql)))

   (str/replace table-stmt #"\)$" (str ", " unique-sql ")"))

arichiardi avatar Feb 01 '18 19:02 arichiardi

@krish8664 any advice/recommendation on this one?

arichiardi avatar Feb 20 '18 19:02 arichiardi

@arichiardi I think the idea would be to come up with a way to specify constraints outside of the inlined way it is possible right now. Let me think about this for a bit. Ideally I would like something of the following syntax

(-> (create-table :films)
    (with-columns [[:a :integer]
                   [:b :integer]
                   [:c :integer])
    (constrain [:unique [:a :c]]))

This should be possible to achieve. Alternatively if you have found a better way to do this, I would like to hear about that.

P.S: Sorry about the extremely delayed response, I haven't been very active here on github for quite sometime. This would change going forward.

ukriish avatar Sep 29 '18 12:09 ukriish

Agree that what you wrote feels right, basically a way to add constraints to columns.

I see inline would be difficult to make it work.

PS.: Good to have you back!

arichiardi avatar Sep 30 '18 01:09 arichiardi