Skip to content

Commit

Permalink
fix: model meta generator doesn't correctly identify relation names (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Apr 12, 2024
1 parent 1ab9d6e commit 4c7fbd4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/sdk/src/model-meta-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function getBackLink(field: DataModelField) {
}

function getRelationName(field: DataModelField) {
const relAttr = field.attributes.find((attr) => attr.decl.ref?.name === 'relation');
const relAttr = field.attributes.find((attr) => attr.decl.ref?.name === '@relation');
const relName = relAttr && relAttr.args?.[0] && getLiteral<string>(relAttr.args?.[0].value);
return relName;
}
Expand Down
88 changes: 88 additions & 0 deletions tests/integration/tests/regression/issue-1241.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { loadSchema } from '@zenstackhq/testtools';
import { randomBytes } from 'crypto';

describe('issue 1241', () => {
it('regression', async () => {
const { enhance, prisma } = await loadSchema(
`
model User {
id String @id @default(uuid())
todos Todo[]
@@auth
@@allow('all', true)
}
model Todo {
id String @id @default(uuid())
user_id String
user User @relation(fields: [user_id], references: [id])
images File[] @relation("todo_images")
documents File[] @relation("todo_documents")
@@allow('all', true)
}
model File {
id String @id @default(uuid())
s3_key String @unique
label String
todo_image_id String?
todo_image Todo? @relation("todo_images", fields: [todo_image_id], references: [id])
todo_document_id String?
todo_document Todo? @relation("todo_documents", fields: [todo_document_id], references: [id])
@@allow('all', true)
}
`,
{ logPrismaQuery: true }
);

const user = await prisma.user.create({
data: {},
});
await prisma.todo.create({
data: {
user_id: user.id,

images: {
create: new Array(3).fill(null).map((_, i) => ({
s3_key: randomBytes(8).toString('hex'),
label: `img-label-${i + 1}`,
})),
},

documents: {
create: new Array(3).fill(null).map((_, i) => ({
s3_key: randomBytes(8).toString('hex'),
label: `doc-label-${i + 1}`,
})),
},
},
});

const db = enhance();

const todo = await db.todo.findFirst({ where: {}, include: { documents: true } });
await expect(
db.todo.update({
where: { id: todo.id },
data: {
documents: {
update: todo.documents.map((doc: any) => {
return {
where: { s3_key: doc.s3_key },
data: { label: 'updated' },
};
}),
},
},
include: { documents: true },
})
).toResolveTruthy();
});
});

0 comments on commit 4c7fbd4

Please sign in to comment.