P
Prisma•4mo ago
Flurrih

[Prisma][MongoDB][Lucia] Malformed objectID

Hi, Im getting very frustrated with the issue Im getting. I was running google oauth on SQLite, but just remapped into MongoDB. I dont know if its Lucia or Prisma... But Im getting this error:
Inconsistent column data: Malformed ObjectID: invalid character 't' was found at index 0 in the provided hex string: "t6d6pgyzluujpbeawuees2f6e6j66el62n43fmd3" for the field 'id'.
.... {
code: 'P2023',
clientVersion: '5.17.0',
meta: {
modelName: 'Session',
message: `Malformed ObjectID: invalid character 't' was found at index 0 in the provided hex string: "t6d6pgyzluujpbeawuees2f6e6j66el62n43fmd3" for the field 'id'.`
}
}
Inconsistent column data: Malformed ObjectID: invalid character 't' was found at index 0 in the provided hex string: "t6d6pgyzluujpbeawuees2f6e6j66el62n43fmd3" for the field 'id'.
.... {
code: 'P2023',
clientVersion: '5.17.0',
meta: {
modelName: 'Session',
message: `Malformed ObjectID: invalid character 't' was found at index 0 in the provided hex string: "t6d6pgyzluujpbeawuees2f6e6j66el62n43fmd3" for the field 'id'.`
}
}
And this is my schema:
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
googleId String @unique
name String?
email String @unique
sessions Session[]
}

model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String @db.ObjectId
expiresAt DateTime

user User @relation(references: [id], fields: [userId], onDelete: Cascade)
}
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
googleId String @unique
name String?
email String @unique
sessions Session[]
}

model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String @db.ObjectId
expiresAt DateTime

user User @relation(references: [id], fields: [userId], onDelete: Cascade)
}
Anyone has idea how to fix this issue? 🙂 Tried many things, but nothing worked so far.
2 Replies
Nurul
Nurul•4mo ago
Hey 👋 How did you generate this value "t6d6pgyzluujpbeawuees2f6e6j66el62n43fmd3"? The error message is because it's not in the format that MongoDB expects.
Flurrih
FlurrihOP•4mo ago
it seems like the schema is generating it:
id String @id @default(auto()) @map("_id") @db.ObjectId
id String @id @default(auto()) @map("_id") @db.ObjectId
with
@default(auto())
@default(auto())
, but then it expects
@db.ObjectId
@db.ObjectId
and it throws the error. I fixed the issue by doing this:
model Session {
id String @id @default(uuid()) @map("_id")
userId String
expiresAt DateTime

user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(uuid()) @map("_id")
googleId String @unique
name String?
email String @unique
sessions Session[]
}
model Session {
id String @id @default(uuid()) @map("_id")
userId String
expiresAt DateTime

user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(uuid()) @map("_id")
googleId String @unique
name String?
email String @unique
sessions Session[]
}
so Im using uuid() and String - not native MongoDB ObjectID, and it works... Perhaps maybe theres some bug with Prisma somewhere for MongoDB? The User that had the ObjectID was correctly created, but the Session is failing on the ObjectIDs - might be due to userId being reference to ObjectId and have two IDs in one schema? but then, Im using Lucia:
const session = await lucia.createSession(existingGoogleUser.id, {});
const session = await lucia.createSession(existingGoogleUser.id, {});
So Im not sure if its Prisma's id or Lucia's tbh But I did some debuging around, and for me it looks like Prisma, but its my first time doing those stuff so not sure
Want results from more Discord servers?
Add your server