Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,10 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
) => {
for (const item of enumerate(args.data)) {
if (args.skipDuplicates) {
if (await this.hasDuplicatedUniqueConstraint(model, item, db)) {
// get a reversed query to include fields inherited from upstream mutation,
// it'll be merged with the create payload for unique constraint checking
const reversedQuery = this.utils.buildReversedQuery(context);
if (await this.hasDuplicatedUniqueConstraint(model, { ...reversedQuery, ...item }, db)) {
if (this.shouldLogQuery) {
this.logger.info(`[policy] \`createMany\` skipping duplicate ${formatObject(item)}`);
}
Expand Down
56 changes: 56 additions & 0 deletions tests/integration/tests/regression/issue-1162.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1162', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model User {
id String @id @default(cuid())
companies CompanyUser[]
@@allow('all', true)
}
model Company {
id String @id @default(cuid())
users CompanyUser[]
@@allow('all', true)
}
model CompanyUser {
company Company @relation(fields: [companyId], references: [id], onDelete: Cascade)
companyId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
@@id([companyId, userId])
@@allow('all', true)
}
`,
{ logPrismaQuery: true }
);

const db = enhance();

await db.user.create({ data: { id: 'abc' } });
await db.user.create({ data: { id: 'def' } });
await db.company.create({ data: { id: '1', users: { create: { userId: 'abc' } } } });
await expect(
db.company.update({
where: { id: '1' },
data: {
users: {
createMany: {
data: [{ userId: 'abc' }, { userId: 'def' }],
skipDuplicates: true,
},
},
},
include: { users: true },
})
).resolves.toMatchObject({
users: expect.arrayContaining([
{ companyId: '1', userId: 'abc' },
{ companyId: '1', userId: 'def' },
]),
});
});
});