02/09/2022, 7:34 PM
I’m running into an issue using references where the id is a uuid string. I have a user model with:
model User {
  id        String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  request   Request?
  createdAt DateTime @default(now()) @db.Timestamptz(6)
  updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6)

model Request {
  id                Int @id @default(autoincrement())
  user              User   @relation(fields: [id], references: [id])
  // I also tried making the relation field userId
  createdAt         DateTime @default(now()) @db.Timestamptz(6)
  updatedAt         DateTime @default(now()) @updatedAt @db.Timestamptz(6)
When I try to migrate this, I get an error that says:
failed to apply cleanly to the shadow database. Error: db error: ERROR: foreign key constraint “Request_userId_fkey” cannot be implemented DETAIL: Key columns “userId” and “id” are of incompatible types: text and uuid.
The prisma documents dont show an example using uuid. The example they do give has a second parameter in the Profile model which has a userId as an Int. I tried adding this to my Request model (as an int, as a string and as a uuid). None of these worked.
model User {
  id      Int      @id @default(autoincrement())
  email   String   @unique
  name    String?
  role    Role     @default(USER)
  posts   Post[]
  profile Profile?

model Profile {
  id     Int    @id @default(autoincrement())
  bio    String
  user   User   @relation(fields: [userId], references: [id])
  userId Int
How can I reference a userId when it is generated using uuid? thank you

Austin Zentz

02/09/2022, 7:40 PM
Does request belong to a user?
yeah, i think so -- I think what you'd want is a field like this under your Request model:
userId String @db.Uuid
and then your user field in the Request model would be:
user User @relation(fields: [profileId], references: [id])
so you want both an actual database field (userId) AND a virtual field (user) that Prisma will use