Generated columns #8891
-
Hello, Sorry in advance if question is basic, I am a beginner. Is there a way, in the schema.prisma definition, to create a column which is always the result of a specific operation of other columns ? e.g. column 1 = Sum ( column 2 + column 3) ? So that if column 3 is modified, column 1 will automatically be updated as well. Thank you in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
If you're using Postgres, you can use it's feature of generated columns with Prisma Migrate. For e.g. Create a schema: model User {
id Int @id @default(autoincrement())
firstName String
lastName String?
fullName String?
} Run Edit the -- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"firstName" TEXT NOT NULL,
"lastName" TEXT,
"fullName" TEXT GENERATED ALWAYS AS ("firstName" || ' ' || "lastName") STORED,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
); Run Now you will get the result of |
Beta Was this translation helpful? Give feedback.
@laurent512 👋
If you're using Postgres, you can use it's feature of generated columns with Prisma Migrate.
For e.g.
Create a schema:
Run
prisma migrate dev --create-only
Edit the
.sql
file to usegenerated
:Run
prisma migrate dev
.Now you will get the result of
fullName
always generated.