Hi all, my Prisma schema is pretty simple and by-t...
# orm-help
m
Hi all, my Prisma schema is pretty simple and by-the-book so far, but it's already giving a Malformed ObjectID error when I try to use it. What am I missing?
Copy code
generator client {
  provider = "prisma-client-js"
}

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

model User {
  id           String      @id @default(auto()) @map("_id") @db.ObjectId
  email        String      @unique(map: "email_1")
  name         String      @unique
  passHash     String
  privilege    Int
  createdAt    DateTime
  lastLogin    DateTime?
  campaigns    Campaign[]
  passcodes    String[]
  disabledCmp  String[]
}

model Campaign {
  id            String     @id @default(auto()) @map("_id") @db.ObjectId
  name          String     @unique
  creator       User       @relation(fields: [creatorId], references: [id])
  creatorId     String     @db.ObjectId
  fullPasscode  String
  pcPasscode    String
  description   String?
  createdAt     DateTime
}
👀 1
Beginning of exact error:
Copy code
PrismaClientKnownRequestError: 
Invalid `prisma.campaign.findMany()` invocation:


  Inconsistent column data: Malformed ObjectID: provided hex string representation must be exactly 12 bytes, instead got: "undefined", length 9.
The Campaign Collection in the db is still empty btw.
j
Prisma is telling you that one of the entries returned does not have an
ObjectId
in one of the columns that expect one, so either
id
or
creatorId
. I would guess there is a missing value in
creatorId
.
m
OK ... that helps a bit, but how could there be a missing creatorId when there aren't any documents yet in the Campaigns collection?
@janpio Hmmm, I deleted all my Users too and re-made them to make sure their ObjectId got populated automatically by prisma.create ... still getting the same error when I try to GET a User.
j
That is indeed weird.
So when the tables are empty you are not getting the error I assume.
And then you create an entry, and instantly get the error?
Can you share the Mongo data of just 1 entry when you created it?
(I think our error message there is actually bad, and we should let you know which column. So if we can reproduce this, I can create an issue on our side to improve this message)
v
👋 hey @McKay Bonham! Did you have a chance to see Jan's comment? Let us know if you still have any issues!