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(polymorphism): wrong query result when selecting "_count" #1124

Merged
merged 1 commit into from
Mar 11, 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
56 changes: 36 additions & 20 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1106,23 +1106,31 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

const modelInfo = getModelInfo(this.options.modelMeta, model, true);

for (const field of Object.values(modelInfo.fields)) {
for (const [key, value] of Object.entries(entity)) {
if (key.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
continue;
}

const field = modelInfo.fields[key];
if (!field) {
// not a field, could be `_count`, `_sum`, etc.
result[key] = value;
continue;
}

if (field.inheritedFrom) {
// already merged from base
continue;
}

if (field.name in entity) {
const fieldValue = entity[field.name];
if (field.isDataModel) {
if (Array.isArray(fieldValue)) {
result[field.name] = fieldValue.map((item) => this.assembleUp(field.type, item));
} else {
result[field.name] = this.assembleUp(field.type, fieldValue);
}
if (field.isDataModel) {
if (Array.isArray(value)) {
result[field.name] = value.map((item) => this.assembleUp(field.type, item));
} else {
result[field.name] = fieldValue;
result[field.name] = this.assembleUp(field.type, value);
}
} else {
result[field.name] = value;
}
}

Expand All @@ -1147,18 +1155,26 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}
}

for (const field of Object.values(modelInfo.fields)) {
if (field.name in entity) {
const fieldValue = entity[field.name];
if (field.isDataModel) {
if (Array.isArray(fieldValue)) {
result[field.name] = fieldValue.map((item) => this.assembleDown(field.type, item));
} else {
result[field.name] = this.assembleDown(field.type, fieldValue);
}
for (const [key, value] of Object.entries(entity)) {
if (key.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
continue;
}

const field = modelInfo.fields[key];
if (!field) {
// not a field, could be `_count`, `_sum`, etc.
result[key] = value;
continue;
}

if (field.isDataModel) {
if (Array.isArray(value)) {
result[field.name] = value.map((item) => this.assembleDown(field.type, item));
} else {
result[field.name] = fieldValue;
result[field.name] = this.assembleDown(field.type, value);
}
} else {
result[field.name] = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression for issue 1123', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model Content {
id String @id @default(cuid())
published Boolean @default(false)
contentType String
likes Like[]
@@delegate(contentType)
@@allow('all', true)
}

model Post extends Content {
title String
}

model Image extends Content {
url String
}

model Like {
id String @id @default(cuid())
content Content @relation(fields: [contentId], references: [id])
contentId String
@@allow('all', true)
}
`
);

const db = enhance();
await db.post.create({
data: {
title: 'a post',
likes: { create: {} },
},
});

await expect(db.content.findFirst({ include: { _count: { select: { likes: true } } } })).resolves.toMatchObject(
{
_count: { likes: 1 },
}
);
});
});
Loading