Mulitple index for json field

I want to add index per json field's sub field I am using MongoDB with Prisma Here is the schema definition
model Chat {
id String @id @default(auto()) @map("_id") @db.ObjectId
messages Json[]
description String?
creatorId String @db.ObjectId
metadata IChatMetadata?

createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt

creator User @relation(fields: [creatorId], references: [id])

@@index([creatorId])
@@index([metadata(path: "published")], map: "ai_chats_metadata_published_idx")
@@index([metadata(path: "isPublic")], map: "ai_chats_metadata_isPublic_idx")
@@map("ai_chats")
}

type IChatMetadata {
gitUrl String?
gitBranch String?
customUrl String?
published Boolean? @default(false)
isPublic Boolean? @default(true)
}
model Chat {
id String @id @default(auto()) @map("_id") @db.ObjectId
messages Json[]
description String?
creatorId String @db.ObjectId
metadata IChatMetadata?

createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt

creator User @relation(fields: [creatorId], references: [id])

@@index([creatorId])
@@index([metadata(path: "published")], map: "ai_chats_metadata_published_idx")
@@index([metadata(path: "isPublic")], map: "ai_chats_metadata_isPublic_idx")
@@map("ai_chats")
}

type IChatMetadata {
gitUrl String?
gitBranch String?
customUrl String?
published Boolean? @default(false)
isPublic Boolean? @default(true)
}
But I got this error
Error parsing attribute "@@index": Index already exists in the model.Prisma
Error parsing attribute "@@index": The given constraint name `ai_chats_metadata_idx` has to be unique in the following namespace: on model `AiChat` for indexes and unique constraints. Please provide a different name using the `map` argument.Prisma
Error parsing attribute "@@index": Index already exists in the model.Prisma
Error parsing attribute "@@index": The given constraint name `ai_chats_metadata_idx` has to be unique in the following namespace: on model `AiChat` for indexes and unique constraints. Please provide a different name using the `map` argument.Prisma
How can I fix this problem?
2 Replies
Prisma AI Help
Howdy, friend! I'm the Prisma AI Help Bot — fast answers from me, or slow, hand-crafted wisdom from a dev? Choose wisely, adventurer.
Nurul
Nurul4w ago
Could you try defining index like this?
@@index([metadata.published], map: "ai_chats_metadata_published_idx")
@@index([metadata.isPublic], map: "ai_chats_metadata_isPublic_idx")
@@index([metadata.published], map: "ai_chats_metadata_published_idx")
@@index([metadata.isPublic], map: "ai_chats_metadata_isPublic_idx")
https://www.prisma.io/docs/orm/prisma-schema/data-model/models#defining-composite-type-indexes

Did you find this page helpful?