Skip to content

Commit c38469c

Browse files
committed
safety check
1 parent 632f10e commit c38469c

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

packages/runtime/src/enhancements/node/delegate.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,34 +1205,36 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
12051205

12061206
const cascadeDeletes: Array<{ model: string; entity: any }> = [];
12071207
const fields = getFields(this.options.modelMeta, model);
1208-
for (const fieldInfo of Object.values(fields)) {
1209-
if (!fieldInfo.isDataModel) {
1210-
continue;
1211-
}
1208+
if (fields) {
1209+
for (const fieldInfo of Object.values(fields)) {
1210+
if (!fieldInfo.isDataModel) {
1211+
continue;
1212+
}
12121213

1213-
if (fieldInfo.isRelationOwner) {
1214-
// this side of the relation owns the foreign key,
1215-
// so it won't cause cascade delete to the other side
1216-
continue;
1217-
}
1214+
if (fieldInfo.isRelationOwner) {
1215+
// this side of the relation owns the foreign key,
1216+
// so it won't cause cascade delete to the other side
1217+
continue;
1218+
}
12181219

1219-
if (fieldInfo.backLink) {
1220-
// get the opposite side of the relation
1221-
const backLinkField = this.queryUtils.getModelField(fieldInfo.type, fieldInfo.backLink);
1222-
1223-
if (backLinkField?.isRelationOwner && this.isFieldCascadeDelete(backLinkField)) {
1224-
// if the opposite side of the relation is to be cascade deleted,
1225-
// recursively delete the delegate base entities
1226-
const relationModel = getModelInfo(this.options.modelMeta, fieldInfo.type);
1227-
if (relationModel?.baseTypes && relationModel.baseTypes.length > 0) {
1228-
// the relation model has delegate base, cascade the delete to the base
1229-
const relationEntities = await db[relationModel.name].findMany({
1230-
where: { [backLinkField.name]: where },
1231-
select: this.queryUtils.makeIdSelection(relationModel.name),
1232-
});
1233-
relationEntities.forEach((entity) => {
1234-
cascadeDeletes.push({ model: fieldInfo.type, entity });
1235-
});
1220+
if (fieldInfo.backLink) {
1221+
// get the opposite side of the relation
1222+
const backLinkField = this.queryUtils.getModelField(fieldInfo.type, fieldInfo.backLink);
1223+
1224+
if (backLinkField?.isRelationOwner && this.isFieldCascadeDelete(backLinkField)) {
1225+
// if the opposite side of the relation is to be cascade deleted,
1226+
// recursively delete the delegate base entities
1227+
const relationModel = getModelInfo(this.options.modelMeta, fieldInfo.type);
1228+
if (relationModel?.baseTypes && relationModel.baseTypes.length > 0) {
1229+
// the relation model has delegate base, cascade the delete to the base
1230+
const relationEntities = await db[relationModel.name].findMany({
1231+
where: { [backLinkField.name]: where },
1232+
select: this.queryUtils.makeIdSelection(relationModel.name),
1233+
});
1234+
relationEntities.forEach((entity) => {
1235+
cascadeDeletes.push({ model: fieldInfo.type, entity });
1236+
});
1237+
}
12361238
}
12371239
}
12381240
}

0 commit comments

Comments
 (0)