Skip to content

fix: model meta generator doesn't correctly identify relation names #1244

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

Merged
merged 1 commit into from
Apr 12, 2024
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
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();
});
});