Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested "create" in "update" results in invalid Prisma query #714

Closed
ymc9 opened this issue Sep 29, 2023 · 0 comments
Closed

Nested "create" in "update" results in invalid Prisma query #714

ymc9 opened this issue Sep 29, 2023 · 0 comments

Comments

@ymc9
Copy link
Member

ymc9 commented Sep 29, 2023

Schema

        model User {
            id Int @id @default(autoincrement())
            username String @unique
        
            employedBy CompanyUser[]
            properties PropertyUser[]
            companies Company[]
        
            @@allow('all', true)
        }
        
        model Company {
            id Int @id @default(autoincrement())
            name String
        
            companyUsers CompanyUser[]
            propertyUsers User[]
            properties Property[]
        
            @@allow('all', true)
        }
        
        model CompanyUser {
            company Company @relation(fields: [companyId], references: [id])
            companyId Int
            user User @relation(fields: [userId], references: [id])
            userId Int
        
            dummyField String
        
            @@id([companyId, userId])
        
            @@allow('all', true)
        }
        
        enum PropertyUserRoleType {
            Owner
            Administrator
        }
        
        model PropertyUserRole {
            id Int @id @default(autoincrement())
            type PropertyUserRoleType
        
            user PropertyUser @relation(fields: [userId], references: [id])
            userId Int
        
            @@allow('all', true)
        }
        
        model PropertyUser {
            id Int @id @default(autoincrement())
            dummyField String
        
            property Property @relation(fields: [propertyId], references: [id])
            propertyId Int
            user User @relation(fields: [userId], references: [id])
            userId Int
        
            roles PropertyUserRole[]
        
            @@unique([propertyId, userId])
        
            @@allow('all', true)
        }
        
        model Property {
            id Int @id @default(autoincrement())
            name String
        
            users PropertyUser[]
            company Company @relation(fields: [companyId], references: [id])
            companyId Int
        
            @@allow('all', true)
        } 

Code

        await db.user.create({
            data: {
                username: 'test@example.com',
            },
        });

        await db.company.create({
            data: {
                name: 'My Company',
                companyUsers: {
                    create: {
                        dummyField: '',
                        user: {
                            connect: {
                                id: 1,
                            },
                        },
                    },
                },
                propertyUsers: {
                    connect: {
                        id: 1,
                    },
                },
                properties: {
                    create: [
                        {
                            name: 'Test',
                        },
                    ],
                },
            },
        });

        await db.property.update({
            data: {
                users: {
                    create: {
                        dummyField: '',
                        roles: {
                            createMany: {
                                data: {
                                    type: 'Owner',
                                },
                            },
                        },
                        user: {
                            connect: {
                                id: 1,
                            },
                        },
                    },
                },
            },
            where: {
                id: 1,
            },
        });

Error

  "Error calling enhanced Prisma method `update`: ",
          "Invalid `db[model].create()` invocation in",
          "node_modules\\@zenstackhq\\runtime\\enhancements\\policy\\handler.js:328:44",
          "",
          "  325 if (this.shouldLogQuery) {",
          "  326     this.logger.info(`[policy] \\`create\\` ${model}: ${(0, utils_1.formatObject)(createArgs)}`);",
          "  327 }",
          "→ 328 const result = yield db[model].create({",
          "        data: {",
          "          dummyField: \"\",",
          "          user: {",
          "            connect: {",
          "              id: 1",
          "            }",
          "          },",
          "          roles: {",
          "            createMany: {",
          "              data: [",
          "                {",
          "                  type: \"Owner\"",
          "                }",
          "              ]",
          "            }",
          "          },",
          "          propertyId: 1,",
          "      +   property: {",
          "      +     create: PropertyCreateWithoutUsersInput | PropertyUncheckedCreateWithoutUsersInput,",
          "      +     connectOrCreate: PropertyCreateOrConnectWithoutUsersInput,",
          "      +     connect: PropertyWhereUniqueInput",
          "      +   }",
          "        },",
          "        select: {",
          "          id: true,",
          "          roles: {",
          "            select: {",
          "              id: true",
          "            }",
          "          }",
          "        }",
          "      })",
          "",
          "Argument `property` is missing.",
          "    at Object.updateOneProperty

Repro

https://github.com/f8k8/ZenstackBugTests/tree/connect_bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant