Skip to content

Commit fbd70c6

Browse files
committed
fix: only validate FKs when query changes them
1 parent 45fd4de commit fbd70c6

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

packages/generator/src/fileCreators/prisma-idb-client/classes/models/api/update.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function addFkValidation(writer: CodeBlockWriter, model: Model) {
490490
whereUnique = `{ ${dependentModelField.relationToFields?.join("_")}: { ${dependentModelField.relationToFields?.map((_field, idx) => `${_field}: record.${dependentModelField.relationFromFields?.at(idx)}`).join(", ")} } }`;
491491
}
492492

493-
let condition = `record.${dependentModelField.relationFromFields?.at(0)} !== undefined`;
493+
let condition = `query.data.${dependentModelField.relationFromFields?.at(0)} !== undefined`;
494494
if (!dependentModelField.isRequired)
495495
condition += ` && record.${dependentModelField.relationFromFields?.at(0)} !== null`;
496496

packages/usage/src/prisma/prisma-idb/prisma-idb-client.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,11 +3856,11 @@ class UserGroupIDBClass extends BaseIDBModelClass<"UserGroup"> {
38563856
);
38573857
}
38583858
}
3859-
if (record.groupId !== undefined) {
3859+
if (query.data.groupId !== undefined) {
38603860
const related = await this.client.group.findUnique({ where: { id: record.groupId } }, tx);
38613861
if (!related) throw new Error("Related record not found");
38623862
}
3863-
if (record.userId !== undefined) {
3863+
if (query.data.userId !== undefined) {
38643864
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
38653865
if (!related) throw new Error("Related record not found");
38663866
}
@@ -4472,7 +4472,7 @@ class ProfileIDBClass extends BaseIDBModelClass<"Profile"> {
44724472
);
44734473
}
44744474
}
4475-
if (record.userId !== undefined) {
4475+
if (query.data.userId !== undefined) {
44764476
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
44774477
if (!related) throw new Error("Related record not found");
44784478
}
@@ -5408,7 +5408,7 @@ class PostIDBClass extends BaseIDBModelClass<"Post"> {
54085408
);
54095409
}
54105410
}
5411-
if (record.authorId !== undefined && record.authorId !== null) {
5411+
if (query.data.authorId !== undefined && record.authorId !== null) {
54125412
const related = await this.client.user.findUnique({ where: { id: record.authorId } }, tx);
54135413
if (!related) throw new Error("Related record not found");
54145414
}
@@ -6192,11 +6192,11 @@ class CommentIDBClass extends BaseIDBModelClass<"Comment"> {
61926192
);
61936193
}
61946194
}
6195-
if (record.postId !== undefined) {
6195+
if (query.data.postId !== undefined) {
61966196
const related = await this.client.post.findUnique({ where: { id: record.postId } }, tx);
61976197
if (!related) throw new Error("Related record not found");
61986198
}
6199-
if (record.userId !== undefined) {
6199+
if (query.data.userId !== undefined) {
62006200
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
62016201
if (!related) throw new Error("Related record not found");
62026202
}
@@ -7922,14 +7922,14 @@ class FatherIDBClass extends BaseIDBModelClass<"Father"> {
79227922
record.userId = null;
79237923
}
79247924
}
7925-
if (record.motherFirstName !== undefined) {
7925+
if (query.data.motherFirstName !== undefined) {
79267926
const related = await this.client.mother.findUnique(
79277927
{ where: { firstName_lastName: { firstName: record.motherFirstName, lastName: record.motherLastName } } },
79287928
tx,
79297929
);
79307930
if (!related) throw new Error("Related record not found");
79317931
}
7932-
if (record.userId !== undefined && record.userId !== null) {
7932+
if (query.data.userId !== undefined && record.userId !== null) {
79337933
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
79347934
if (!related) throw new Error("Related record not found");
79357935
}
@@ -9156,7 +9156,7 @@ class MotherIDBClass extends BaseIDBModelClass<"Mother"> {
91569156
record.userId = null;
91579157
}
91589158
}
9159-
if (record.userId !== undefined && record.userId !== null) {
9159+
if (query.data.userId !== undefined && record.userId !== null) {
91609160
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
91619161
if (!related) throw new Error("Related record not found");
91629162
}
@@ -10283,18 +10283,18 @@ class ChildIDBClass extends BaseIDBModelClass<"Child"> {
1028310283
);
1028410284
}
1028510285
}
10286-
if (record.userId !== undefined && record.userId !== null) {
10286+
if (query.data.userId !== undefined && record.userId !== null) {
1028710287
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
1028810288
if (!related) throw new Error("Related record not found");
1028910289
}
10290-
if (record.fatherFirstName !== undefined) {
10290+
if (query.data.fatherFirstName !== undefined) {
1029110291
const related = await this.client.father.findUnique(
1029210292
{ where: { firstName_lastName: { firstName: record.fatherFirstName, lastName: record.fatherLastName } } },
1029310293
tx,
1029410294
);
1029510295
if (!related) throw new Error("Related record not found");
1029610296
}
10297-
if (record.motherFirstName !== undefined) {
10297+
if (query.data.motherFirstName !== undefined) {
1029810298
const related = await this.client.mother.findUnique(
1029910299
{ where: { firstName_lastName: { firstName: record.motherFirstName, lastName: record.motherLastName } } },
1030010300
tx,

0 commit comments

Comments
 (0)