prisma: `Gist` index not supported on prisma schema (Exclusion constraint)

Bug description

Hello

I’m introspecting my database and this is the schema that is being generated when I use the prisma cli and run npx prisma db pull.

model appointment {
  account_id String                   @default(dbgenerated("(current_setting('app.account'::text))::uuid")) @db.Uuid
  id         String                   @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  date       Unsupported("tstzrange")
  patient_id String                   @db.Uuid
  is_paid    Boolean                  @default(false)
  is_active  Boolean                  @default(true)
  created_at DateTime                 @default(now()) @db.Timestamptz(6)
  patient    patient                  @relation(fields: [account_id, patient_id], references: [account_id, id], onDelete: NoAction, onUpdate: NoAction, map: "FK_Appointment_patient_id__Patient_id")
  account    account                  @relation(fields: [account_id], references: [id], onDelete: NoAction, onUpdate: NoAction)

  @@id([account_id, id], map: "PK_Appointment_account_id_id")
  @@index([account_id, date], map: "appointment_account_id_date_excl", type: Gist)
}

I get the following error once I try to generate the client by running npx prisma generate.

This is the error.

Error: Schema validation error - Error (query-engine-node-api library)
Error code: P1012
error: Error parsing attribute "@@index": The Gist index field type `Uuid` has no default operator class.
  -->  schema.prisma:54
   | 
53 |   @@id([account_id, id], map: "PK_Appointment_account_id_id")
54 |   @@index([account_id, date], map: "appointment_account_id_date_excl", type: Gist)
   | 

Validation Error Count: 1
[Context: getDmmf]

I thought Gist indexes where supported as stated on https://www.prisma.io/docs/concepts/components/prisma-schema/indexes#index-configuration

How to reproduce

Try to generate the prisma client based from this schema, use the cli npx prisma generate.

model appointment {
  account_id String                   @default(dbgenerated("(current_setting('app.account'::text))::uuid")) @db.Uuid
  id         String                   @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  date       Unsupported("tstzrange")
  patient_id String                   @db.Uuid
  is_paid    Boolean                  @default(false)
  is_active  Boolean                  @default(true)
  created_at DateTime                 @default(now()) @db.Timestamptz(6)
  patient    patient                  @relation(fields: [account_id, patient_id], references: [account_id, id], onDelete: NoAction, onUpdate: NoAction, map: "FK_Appointment_patient_id__Patient_id")
  account    account                  @relation(fields: [account_id], references: [id], onDelete: NoAction, onUpdate: NoAction)

  @@id([account_id, id], map: "PK_Appointment_account_id_id")
  @@index([account_id, date], map: "appointment_account_id_date_excl", type: Gist)
}

Expected behavior

The prisma client is generated with no errors.

Prisma information

model appointment {
  account_id String                   @default(dbgenerated("(current_setting('app.account'::text))::uuid")) @db.Uuid
  id         String                   @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  date       Unsupported("tstzrange")
  patient_id String                   @db.Uuid
  is_paid    Boolean                  @default(false)
  is_active  Boolean                  @default(true)
  created_at DateTime                 @default(now()) @db.Timestamptz(6)
  patient    patient                  @relation(fields: [account_id, patient_id], references: [account_id, id], onDelete: NoAction, onUpdate: NoAction, map: "FK_Appointment_patient_id__Patient_id")
  account    account                  @relation(fields: [account_id], references: [id], onDelete: NoAction, onUpdate: NoAction)

  @@id([account_id, id], map: "PK_Appointment_account_id_id")
  @@index([account_id, date], map: "appointment_account_id_date_excl", type: Gist)
}

Environment & setup

  • macOS
  • Postgres 14
  • Prisma CLI Version : 4.3.1
  • Node 16.15.1

Prisma Version

4.3.1

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Internal: Next step is to try to reproduce locally what @gmwill934 is seeing, and then figure out what is going on with that ops class.

Absolutely @janpio

Account

create table account (
	id uuid not null default(gen_random_uuid()) primary key,
	email varchar(64) not null unique,
	password varchar(128) not null,
	first_name varchar(32) not null,
	last_name varchar(32) not null,
	city varchar(32) null,
	state varchar(32) null,
	is_verified boolean default(false) not null,
	created_at timestamp with time zone not null default(now())
);

Patient

create table patient (
	account_id uuid not null default(current_setting('app.account')::uuid) references account(id),
	id uuid not null default(gen_random_uuid()),
	email varchar(64) not null,
	first_name varchar(32) not null,
	last_name varchar(32) not null,
	street varchar(64) null,
	interior varchar(16) null,
	exterior varchar(16) null,
	zip varchar(16) null,
	neighborhood varchar(64) null,
	municipality varchar(64) null,
	city varchar(64) null,
	state varchar(64) null,
	tax_system varchar(16) null,
	country varchar (16) null default('MEX'),
	dob date null,
	rfc varchar(13) null,
	legal_name varchar(128) null,
	facturapi_customer_id varchar(32) null,
	is_active boolean not null default(true),
	created_at timestamp with time zone not null default(now()),
	constraint "PK_Patient_account_id_id" primary key (account_id, id),
	constraint "UQ_Patient_account_id_email" unique (account_id, email)
);

Appointment

create table appointment (
	account_id uuid not null default(current_setting('app.account')::uuid) references account(id),
	id uuid not null default(gen_random_uuid()),
	date tstzrange not null,
	patient_id uuid not null,
	is_paid boolean not null default(false),
	is_active boolean not null default(true),
	created_at timestamp with time zone not null default(now()),
	constraint "PK_Appointment_account_id_id" primary key (account_id, id),
	constraint "FK_Appointment_patient_id__Patient_id" foreign key (account_id, patient_id) references patient (account_id, id),
	exclude using gist (account_id WITH =, date WITH &&),
	constraint "CHECK_Appointment_date_lower_not_null" check (lower(date) is not null),
	constraint "CHECK_Appointment_date_lower_not_infinity" check (lower(date) > '-infinity'),
	constraint "CHECK_Appointment_date_upper_not_null" check (upper(date) is not null),
	constraint "CHECK_Appointment_date_upper_not_infinity" check (upper(date) < 'infinity')
);

Yes, @janpio I do have the btree_gist extension activated

We should be better in introspecting these then @gmwill934. Can you share the SQL of the appointment table (and maybe the ones it has relations with, although we can certainly clean that out if not)? Thanks.

Hi @gmwill934, could you please share the rest of your schema? We see that the appointment model references the patient and account models as well.

These are the operator classes of GiST we currently support for Postgres. We currently provide an escape hatch to use a custom operator class in GiST and Gin indexes, via ops:

@@index([account_id(ops: raw("")), date(ops: raw(""))], map: "appointment_account_id_date_excl", type: Gist)

Besides, I see the date field mentioned in the @@index is using the tstzrange extension, which is not supported by Prisma (I would have linked you to https://github.com/prisma/prisma/issues/3287, but I see you have already found that issue on your own ☺️).