Hi I am trying to apply like preoperation on my m...
# orm-help
s
Hi I am trying to apply like preoperation on my model like if I have model like
Copy code
{
a Int
b Int 
c Int 
sum Int
}
is there any way to calculate and assign this `sum as the value of a,b,c`without applying in node js like I see in nosql data
r
@Shubham Singh ๐Ÿ‘‹ Are you using Postgres? If so, you can first create the model in your
schema.prisma
as follows:
Copy code
model Data {
  id  Int @id @default(autoincrement())
  a   Int
  b   Int
  c   Int
  sum Int
}
Then run the following command:
Copy code
prisma migrate dev --create-only --preview-feature
Alter the created
.sql
file in the
migrations
folder to:
Copy code
CREATE TABLE "Data" (
    "id" SERIAL,
    "a" INTEGER NOT NULL,
    "b" INTEGER NOT NULL,
    "c" INTEGER NOT NULL,
    "sum" int generated always as (a + b + c) stored,

    PRIMARY KEY ("id")
);
This will always calculate the sum field as a combination of the three values.
k
generated always as (a + b + c) stored
i have a hard time looking this up on Prisma docs, do you have a link for this? thanks @Ryan
r
This isnโ€™t Prisma specific as itโ€™s a Postgres feature: https://www.postgresql.org/docs/12/ddl-generated-columns.html ๐Ÿ™‚
k
ah got it. thanks!
๐Ÿ‘ 1
s
Thanks ryan for helping me out I was confused with that
๐Ÿ‘ 1