prisma: Cannot use `crdb_region` in CockroachDB
Bug description
I’m using Prisma to query a dedicated CockroachDB multi-region instance. For REGIONAL BY ROW tables, there is a hidden internal crdb_region column created by CockroachDB. This column has an internal enum type crdb_internal_region that is composed of the different cloud regions where the DB Nodes are hosted. In the hosted offering, these are set by CockroachDB to be values like gcp-us-east1 (with dashes) and cannot be changed afaik.
If I try to explicitly insert or read values to crdb_region, then I have to include crdb_region in my Prisma schema for that model for it to show up in the client. However, because crdb_region is represented as an enum in Cockroach, I can’t represent crdb_region as a String type. I tried defining an enum in Prisma like:
enum crdb_internal_region {
gcp-us-east1
gcp-europe-west1
gcp-asia-south1
}
but there is an error since these enum values have dashes in them which are not currently allowed. As of now, I have no way to proceed.
References: [0] https://www.cockroachlabs.com/blog/under-the-hood-multi-region/ [1] https://www.cockroachlabs.com/docs/stable/set-locality.html [2] https://github.com/prisma/prisma/issues/273
Prisma information
Example schema:
model self_stated {
id String
user_id String
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime @default(now()) @db.Timestamptz(6)
users users @relation(fields: [crdb_region, user_id], references: [crdb_region, id], onDelete: Cascade, map: "self_stated_users_id_fk")
crdb_region String // Doesn't work!
user_country_code String
@@id([crdb_region, id])
@@index([user_id], map: "self_stated_user_id_index")
}
Error is:
Attempted to serialize scalar 'gcp-us-east1' with incompatible type 'String' for field crdb_region
Environment & setup
- OS: Debian
- Database: CockroachDB
- Node.js version: v16.13.1
Prisma Version
prisma : 3.8.0
@prisma/client : Not found
Current platform : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../home/node/.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine : migration-engine-cli 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../home/node/.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../home/node/.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary : prisma-fmt 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../home/node/.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash : 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f
Studio : 0.452.0
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 5
- Comments: 16 (4 by maintainers)
I added the following enum to my schema:
and used that type for the
crdb_region:Note that this requirement makes adopting the workaround from https://github.com/prisma/prisma/issues/11317#issuecomment-1066749445 harder since I can’t blanket remove all uses of enums (which are the source of latency variability). My planned (but untested) workaround for now is to issue recurring queries on
crdb_regionevery few seconds to make sure the enum values remained cached to avoid the slow query.Not sure if others have had success creating migrations on schemas with a
crdb_internal_regionenum. Presently (with prisma 5.17.0)prisma migrate deverrors saying my schema has drifted (full reproduction/demo below):https://github.com/ciiqr/prisma-cockroachdb-region-enum
https://github.com/user-attachments/assets/62eea690-aeeb-4a27-810a-6a893df6cca9
For those needing a quick fix (and since I can’t see it mentioned already here), use
prisma migrate dev --create-onlyto create a migration first without actually running it, then edit it manually in the migrations directory and remove theDROP TYPE "crdb_internal_region";line at the top. You can then run the migration withprisma migrate deploy.Yes, this worked for me!
I was expecting this somewhere in the
enumsection of the docs – but thanks for the pointer!Have you tried to use
prisma db pullon that dabase? What schema does that create? Most importantly, does it pick up thecrdb_regioncolumn?Alterantively or additionally:
@mapalso exists for enum values, which enables you to do something like this: