how to define field w/ postgres IDENTITY?

I am trying Drizzle out for the first time. I thought to flex the quick start with realistic production user table schema. I can't find anywhere (including searching GitHub for examples) how to define the following field??
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
2 Replies
rock192
rock19217mo ago
I'm new to drizzle as well, had the same issue. support for identity column is planned but still not implemented meanwhile I found a workaround on their GitHub issue which uses a custom type
import { customType } from "drizzle-orm/pg-core";

export const identity = (name: string) =>
customType<{
data: number;
notNull: true;
default: true;
}>({
dataType() {
return "INTEGER GENERATED ALWAYS AS IDENTITY";
},
})(name);
import { customType } from "drizzle-orm/pg-core";

export const identity = (name: string) =>
customType<{
data: number;
notNull: true;
default: true;
}>({
dataType() {
return "INTEGER GENERATED ALWAYS AS IDENTITY";
},
})(name);
pgTable("table_with_id", {
id: identity("id").primaryKey(),
});
pgTable("table_with_id", {
id: identity("id").primaryKey(),
});
https://github.com/drizzle-team/drizzle-orm/issues/295
iolyd
iolyd17mo ago
Am I mistaken or this custom type does not generate the migration code ArnaudD claims? On my end, generated migrations using this type quote the whole type as a string

Did you find this page helpful?