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

fix(delegate): nested create/connect in delegate model requires reference fields #1901

Merged
merged 1 commit into from
Dec 3, 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
42 changes: 37 additions & 5 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,49 @@ export class PrismaSchemaGenerator {
(attr) => (attr as PrismaFieldAttribute).name !== '@relation'
);

const relKeyPairs = getRelationKeyPairs(f);

if (
// array relation doesn't need FK
f.type.array ||
// FK field is defined on this side
relKeyPairs.length > 0 ||
// opposite relation already has FK, we don't need to generate on this side
(oppositeRelationAttr && getAttributeArg(oppositeRelationAttr, 'fields'))
) {
prismaField.attributes.push(
new PrismaFieldAttribute('@relation', [
new PrismaAttributeArg(undefined, new AttributeArgValue('String', relName)),
])
);
const relationArgs = [new PrismaAttributeArg(undefined, new AttributeArgValue('String', relName))];
const isSelfRelation = f.type.reference.ref === (f.$inheritedFrom ?? f.$container);
if (relKeyPairs.length > 0 && !isSelfRelation) {
// carry over "fields" and "references" args if not a self-relation
relationArgs.push(
new PrismaAttributeArg(
'fields',
new AttributeArgValue(
'Array',
relKeyPairs.map(
(pair) =>
new AttributeArgValue(
'FieldReference',
new PrismaFieldReference(pair.foreignKey.name)
)
)
)
)
);
relationArgs.push(
new PrismaAttributeArg(
'references',
new AttributeArgValue(
'Array',
relKeyPairs.map(
(pair) =>
new AttributeArgValue('FieldReference', new PrismaFieldReference(pair.id.name))
)
)
)
);
}
prismaField.attributes.push(new PrismaFieldAttribute('@relation', relationArgs));
} else {
// generate FK field
const oppositeModelIds = getIdFields(oppositeRelationField.$container as DataModel);
Expand Down
53 changes: 53 additions & 0 deletions tests/regression/tests/issue-1894.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1894', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model A {
id Int @id @default(autoincrement())
b B[]
}

model B {
id Int @id @default(autoincrement())
a A @relation(fields: [aId], references: [id])
aId Int

type String
@@delegate(type)
}

model C extends B {
f String?
}
`,
{
enhancements: ['delegate'],
compile: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import { enhance } from '.zenstack/enhance';
import { PrismaClient } from '@prisma/client';

async function main() {
const db = enhance(new PrismaClient());
await db.a.create({ data: { id: 0 } });
await db.c.create({ data: { a: { connect: { id: 0 } } } });
}

main();

`,
},
],
}
);

const db = enhance();
await db.a.create({ data: { id: 0 } });
await expect(db.c.create({ data: { a: { connect: { id: 0 } } } })).toResolveTruthy();
});
});
Loading