Skip to content

Generated columns #8891

Answered by ryands17
laurent512 asked this question in Q&A
Aug 24, 2021 · 1 comments · 7 replies
Discussion options

You must be logged in to vote

@laurent512 👋

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 prisma migrate dev --create-only

Edit the .sql file to use generated:

-- 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 prisma migrate dev.

Now you will get the result of fullName always generated.

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@laurent512
Comment options

@ryands17
Comment options

@ryands17
Comment options

@tsongas
Comment options

@Bennison-Devadoss
Comment options

Answer selected by jharrell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants