Skip to content

Postgres Introspection occasionally removes "Unique" Constraint #5833

@alexnakagawa

Description

@alexnakagawa

Love the library and very appreciative of all the work that goes into this <3

Bug description

I have a Next.js project with a prisma directory with a schema.sql file with the following PostgresQL syntax:

CREATE TABLE IF NOT EXISTS "public"."Athlete" (
  id SERIAL PRIMARY KEY NOT NULL,
  "espnId" INTEGER,
  "olympediaId" INTEGER,
  "firstName" VARCHAR(255),
  "lastName" VARCHAR(255),
  "nativeName" VARCHAR(255),
  description TEXT,
  "popScore" INTEGER,
  status VARCHAR(255),
  heritage VARCHAR(2) ARRAY,
  slug VARCHAR(255) UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS "public"."User" (
  id SERIAL PRIMARY KEY NOT NULL,
  name VARCHAR(255),
  email VARCHAR(255) UNIQUE NOT NULL
);

As you can see, the slug column in the Athlete table should be created with a @unique constraint when I introspect the schema.prisma. However, this does not happen when I run npx prisma introspect.

model Athlete {
  id              Int               @id @default(autoincrement())
  espnId          Int?
  olympediaId     Int?              @default(0)
  firstName       String?           @db.VarChar(255)
  lastName        String?           @db.VarChar(255)
  nativeName      String?           @db.VarChar(255)
  sport           String            @db.VarChar(3)
  description     String?
  popScore        Int?
  status          String?           @db.VarChar(255)
  heritage        String[]          @db.VarChar(2)
  slug            String            @db.VarChar(255)
  Sport           Sport             @relation(fields: [sport], references: [id])
  FavoriteMoments FavoriteMoments[]
}

How to reproduce

Steps to reproduce the behavior:

  1. Start from this template repo
  2. Run npx prisma init to create prisma folder
  3. Change datasource to as follows:
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
  1. Add schema.sql file with the above information

Expected behavior

Running npx prisma introspect should return the following:

model Athlete {
  id              Int               @id @default(autoincrement())
  espnId          Int?
  olympediaId     Int?              @default(0)
  firstName       String?           @db.VarChar(255)
  lastName        String?           @db.VarChar(255)
  nativeName      String?           @db.VarChar(255)
  sport           String            @db.VarChar(3)
  description     String?
  popScore        Int?
  status          String?           @db.VarChar(255)
  heritage        String[]          @db.VarChar(2)
  slug            String            @unique @db.VarChar(255)
  Sport           Sport             @relation(fields: [sport], references: [id])
  FavoriteMoments FavoriteMoments[]
}

model User {
  id              Int               @id @default(autoincrement())
  name            String?           @db.VarChar(255)
  email           String            @unique @db.VarChar(255)
  FavoriteMoments FavoriteMoments[]
  Post            Post[]
  Profile         Profile?
}

The slug on the Athlete model should contain the @unique constraint, but it has disappeared. Meanwhile, the User model has kept the @unique constraint for email.

Prisma information

Full schema.prisma file as follows:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Athlete {
  id              Int               @id @default(autoincrement())
  espnId          Int?
  olympediaId     Int?              @default(0)
  firstName       String?           @db.VarChar(255)
  lastName        String?           @db.VarChar(255)
  nativeName      String?           @db.VarChar(255)
  sport           String            @db.VarChar(3)
  description     String?
  popScore        Int?
  status          String?           @db.VarChar(255)
  heritage        String[]          @db.VarChar(2)
  slug            String            @db.VarChar(255)
  Sport           Sport             @relation(fields: [sport], references: [id])
  FavoriteMoments FavoriteMoments[]
}

model FavoriteMoments {
  id          Int      @id @default(autoincrement())
  createdAt   DateTime @default(now()) @db.Timestamp(6)
  userId      Int?
  athleteId   Int?
  description String?
  url         String?
  Athlete     Athlete? @relation(fields: [athleteId], references: [id])
  User        User?    @relation(fields: [userId], references: [id])
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String   @db.VarChar(255)
  createdAt DateTime @default(now()) @db.Timestamp(6)
  content   String?
  published Boolean  @default(false)
  authorId  Int
  User      User     @relation(fields: [authorId], references: [id])
}

model Profile {
  id     Int     @id @default(autoincrement())
  bio    String?
  userId Int     @unique
  User   User    @relation(fields: [userId], references: [id])
}

model Sport {
  id      String    @id @db.VarChar(3)
  sport   String    @db.VarChar(255)
  gender  String?   @db.VarChar(255)
  olympic Boolean?
  Athlete Athlete[]
}

model User {
  id              Int               @id @default(autoincrement())
  name            String?           @db.VarChar(255)
  email           String            @unique @db.VarChar(255)
  FavoriteMoments FavoriteMoments[]
  Post            Post[]
  Profile         Profile?
}

Environment & setup

  • OS: macOS 11.1
  • Database: PostgreSQL 11.7
  • Node.js version: v14.15.0
  • Prisma version:
prisma               : 2.17.0
@prisma/client       : 2.17.0
Current platform     : darwin
Query Engine         : query-engine 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.353.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions