Skip to content

Commit eb1a0c2

Browse files
authored
fix(delegate): exclude uninheritable attributes properly (#488)
* fix(delegate): exclude uninheritable attributes properly * fix tests
1 parent 4417d20 commit eb1a0c2

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

packages/language/src/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,20 @@ export function getAllAttributes(
561561

562562
if (isDataModel(decl) && decl.baseModel) {
563563
if (decl.baseModel.ref) {
564-
attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
564+
const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
565+
attributes.push(...attrs);
565566
}
566567
}
567568

568569
attributes.push(...decl.attributes);
569570
return attributes;
570571
}
571572

573+
function isNonInheritableAttribute(attr: DataModelAttribute) {
574+
const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
575+
return ['@@map', '@@unique', '@@index'].includes(attrName);
576+
}
577+
572578
/**
573579
* Retrieve the document in which the given AST node is contained. A reference to the document is
574580
* usually held by the root node of the AST.

packages/sdk/src/prisma/prisma-schema-generator.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ export class PrismaSchemaGenerator {
196196
}
197197
}
198198

199-
const allAttributes = getAllAttributes(decl).filter(
200-
(attr) => this.isPrismaAttribute(attr) && !this.isInheritedMapAttribute(attr, decl),
201-
);
199+
const allAttributes = getAllAttributes(decl).filter((attr) => this.isPrismaAttribute(attr));
202200

203201
for (const attr of allAttributes) {
204202
this.generateContainerAttribute(model, attr);
@@ -241,15 +239,6 @@ export class PrismaSchemaGenerator {
241239
return getStringLiteral(defaultSchemaField?.value) ?? 'public';
242240
}
243241

244-
private isInheritedMapAttribute(attr: DataModelAttribute, contextModel: DataModel) {
245-
if (attr.$container === contextModel) {
246-
return false;
247-
}
248-
249-
const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
250-
return attrName === '@@map';
251-
}
252-
253242
private isPrismaAttribute(attr: DataModelAttribute | DataFieldAttribute) {
254243
if (!attr.decl.ref) {
255244
return false;

tests/e2e/orm/schemas/delegate/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export class SchemaType implements SchemaDef {
145145
}
146146
},
147147
attributes: [
148+
{ name: "@@index", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("ownerId")]) }] },
148149
{ name: "@@delegate", args: [{ name: "discriminator", value: ExpressionUtils.field("assetType") }] }
149150
],
150151
idFields: ["id"],

tests/e2e/orm/schemas/delegate/schema.zmodel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ model Asset {
2828
comments Comment[]
2929
assetType String
3030

31+
@@index([ownerId])
32+
3133
@@delegate(assetType)
3234
}
3335

tests/regression/test/v2-migrated/issue-1243.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Regression for issue 1243', () => {
2424
}
2525
`;
2626

27-
await createTestClient(schema);
27+
await createTestClient(schema, { usePrismaPush: true });
2828
});
2929

3030
it('multiple id fields', async () => {
@@ -47,6 +47,6 @@ describe('Regression for issue 1243', () => {
4747
}
4848
`;
4949

50-
await createTestClient(schema);
50+
await createTestClient(schema, { usePrismaPush: true });
5151
});
5252
});

0 commit comments

Comments
 (0)