:wave: I recently read this excellent write up `Le...
# orm-help
l
👋 I recently read this excellent write up 
Lessor 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:
Copy code
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:
Copy code
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
Copy code
@db.Int @default(autoincrement())
Generates the
SERIAL
column in postgres - is it possible to specify a custom column type?
Copy code
model User {
  id   String  @id 'INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY'
  test String?
}
r
@Leo Romanovsky 👋 You can do this by editing the migration file using the following steps: 1. Create the model with
autoincrement
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.