Is it possible to have @id @default(autoincrement())
with auto-incrementation starting the id
s from 0 instead of 1 ?
In relation to : start ids at an arbitrary number in prisma data model
(which did not receive any answer either)
I am interested to see if it is possible with postgresql
in particular.
Prisma generates the corresponding SQL :
CREATE TABLE "Message" (
"id" SERIAL NOT NULL,
"from" TEXT NOT NULL,
"content" TEXT NOT NULL,
"discussionId" INTEGER NOT NULL,
CONSTRAINT "Message_pkey" PRIMARY KEY ("id")
);
Is the NOT NULL
the issue ? Would it be ok to remove it ?
That SO answer seems to at least suggest that it would be possible : https://stackoverflow.com/a/32728273/10469162
And if it’s possible, is there a reason for Prisma not to expose it ?
2
Answers
It's not possible from the schema directly but you can modify the migration file to achieve this. This example uses a Postgres database.
Let's assume that we have to start the Task id from 0. To do that, Add the model in schema.prisma file
Then run the below command
It will add the migration file in the prisma/migrations folder. Then open the migration.sql file and add the following code
Then run the following command to apply in the database
Then you should see the output like below From here, you can then proceed to create records. For example
Logging the result to the console, you should see the id starting at 0
This response was crafted by ludralph over at github and reproduced here with him being notified about it for the purpose of sharing knowledge. Here is the original link of the answer: https://github.com/prisma/prisma/discussions/18246
There are a couple options available. The first and preferred is generated always as identity. Available only in versions 10 and above.
The other (and required for versions prior to 10) is not define the
id
column as serial, but manually do what serial does. Althoughserial
occupies the place of data type in the ddl it is not a data type; it is actually a short for:So:
See demo. Sorry, but I am unable to translate into your obscurification language (Prisma) as I am not familiar enough with it.