olivierwilkinson / prisma-extension-nested-operations

Utils for creating Prisma client extensions with nested operations
Apache License 2.0
32 stars 6 forks source link

Incompatible to Fluent API of Prisma #9

Open dahaupt opened 6 months ago

dahaupt commented 6 months ago

First of all, thanks a lot for this extension, which is really crucial in our project as part of a multi-tenant validation.

We ran into an issue related to the usage of the Fluent API of Prisma.

Example schema:

model Parent {
  id    Int     @id @default(autoincrement())
  child Child[]
}

model Child {
  id       Int     @id @default(autoincrement())
  child    String
  parent   Parent? @relation(fields: [parentId], references: [id])
  parentId Int?
}

Example relation query:

const child = await prisma.parent.findUniqueOrThrow({
    where: {
        id: 1
    }
}).child();

Expected output:

[
  {
    id: 1,
    child: 'Test',
    parentId: 1,
  }
]

Actual output:

[
  {
    id: 1,
    child: null, // <-- Invalid
    parentId: 1,
  }
]

The problem seems to occur only if a field with the same name as the model is used. The (string) value of the field is then replaced with null (related to this operation).

We solved it simply by changing the field name of the relation to the plural form (childs) in order to have a difference to the field name (child).