sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Add domain support

Open kyleconroy opened this issue 5 years ago • 2 comments

A domain is "essentially a data type with optional constraints". You can think of them as type aliases. By using a domain, you can easily create custom types. For example, here's how you could create a KSUID type.

CREATE DOMAIN ksuid AS VARCHAR(27) NOT NULL;

CREATE TABLE foo (
    id ksuid PRIMARY KEY,
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE bar (
    id ksuid PRIMARY KEY,
    created_at TIMESTAMP DEFAULT NOW(),
    foo_id ksuid REFERENCES foo(id),
    name text NOT NULL,
    slug text NOT NULL UNIQUE
);

The advantage to using a domain is that you'd only need one entry in overrides

overrides:
  - db_type: "ksuid"
    go_type: "github.com/segmentio/ksuid.KSUID"

Replaces #418

kyleconroy avatar Apr 05 '20 18:04 kyleconroy

This looks to be supported, except schema scoped domain types like CREATE DOMAIN public.name TEXT NOT NULL;

stepan-romankov avatar Jul 12 '23 11:07 stepan-romankov

except schema scoped domain

It seems to work too, you just have to include the scope in the db_type

hectorj-thetreep avatar Dec 08 '23 07:12 hectorj-thetreep