Leo Romanovsky
11/11/2021, 9:16 AMLessor Known PostgreSQL Features
https://hakibenita.com/postgresql-unknown-features#prevent-setting-the-value-of-an-auto-generated-key it was a top post on hacker news: https://news.ycombinator.com/item?id=29163319
One of the solid pieces of advice it has was to use an alternative primary key column type:
CREATE TABLE sale (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
sold_at TIMESTAMPTZ,
amount INT
);
This blocks users from being able to write directly to the id
field of a schema like above with an error:
db=# INSERT INTO sale (id, sold_at, amount) VALUES (2, now(), 1000);
ERROR: cannot insert into column "id"
DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.
The
@db.Int @default(autoincrement())
Generates the SERIAL
column in postgres - is it possible to specify a custom column type?
model User {
id String @id 'INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY'
test String?
}
Ryan
11/11/2021, 11:04 AMautoincrement
and run prisma migrate dev --create-only
2. Edit the file and replace serial
with int generated always as identity
3. Run prisma migrate dev
to apply the migration.