0

We are upgrading our app from Mongoose to Prisma with an existing MongoDB schema. The problem is that Prisma does not support relations on fields that are arrays of IDs (String[]), which breaks our attributeFamilies relation in the products model.

Our Prisma schema models relevant to this issue are:

model productattributefamilies {
  id          String              @id @default(auto()) @map("_id") @db.ObjectId
  attributes  productattributes[]
  products    products[]          @relation(fields: [productIds], references: [id])
  productIds  String[]            @db.ObjectId
  // other fields...
}

model products {
  id                 String                     @id @default(auto()) @map("_id") @db.ObjectId
  attributeFamilies  productattributefamilies[] @relation(fields: [attributeFamilyIds], references: [id])
  attributeFamilyIds String[]                   @map("attributeFamilies") @db.ObjectId
  // other fields...
}

Problem

products.attributeFamilyIds is a String[] of ObjectId references to productattributefamilies.

Prisma does not support relations on array fields, so the declared relation:

attributeFamilies productattributefamilies[] @relation(fields: [attributeFamilyIds], references: [id])

is invalid and Prisma will not load nested attributeFamilies.

Our existing Mongoose app relies on populating this array of IDs, and our API returns nested attributeFamilies with their attributes.

When querying Prisma with .include({ attributeFamilies: { include: { attributes: true } } }), attributeFamilies is always empty.

This breaks our API response shape, affecting frontend and consumers.

Requirements

We want full backward compatibility with the existing API response, no changes to frontend or API consumers.

We want the Prisma query to return nested attributeFamilies with attributes exactly as before.

We want to avoid any solution that requires flattening or restructuring data in a way that changes the response format.

Ideally, we want a Prisma-friendly way or a script to prepare data to keep compatibility without changing the app frontend or API contract.

Next Steps / Help Needed

Advice on how to model or query this relation in Prisma given the String[] of IDs.

Workarounds to manually load and nest attributeFamilies if Prisma relations don’t support this pattern.

Scripts or migration strategies to make data Prisma-friendly while preserving backward compatibility.

Any alternative approaches to keep the same query results without breaking the API.

1 Answer 1

1

Prisma and Mongoose are different, relations on array fields work differently, and you shouldn’t use @map("attributeFamilies") on attributeFamilyIds.

Instead, define a named relation like this:

model products {
  id                 String                     @id @default(auto()) @map("_id") @db.ObjectId
  attributeFamilyIds String[]                   @db.ObjectId
  attributeFamilies  productattributefamilies[] @relation("ProductItems", fields: [attributeFamilyIds], references: [id])
  // other fields...
}
model productattributefamilies {
  id                 String                     @id @default(auto()) @map("_id") @db.ObjectId
  productIds String[]   @db.ObjectId
  products   products[] @relation("ProductItems", fields: [productIds], references: [id])
  // other fields...
}

for more information you can check the Prisma Docs.

Note: I haven't tested this in your exact setup, so if it doesn't work, drop a comment and I’ll help debug further.

Sign up to request clarification or add additional context in comments.

1 Comment

After a lot of discussion with the team, we ended up going back to Mongoose with NestJS. It’s well-typed and just worked perfectly for what we needed. Personally, I feel like Prisma is more geared towards SQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.