Kay Khan
06/20/2022, 10:49 AM[entity].create()
and "connect" for relationships..
I have the following input, you can see im attaching 2 relationships when creating this brand
entity. (image
& brand_industry
). - In the brand
table, image
is nullable:
`image_id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
const newBrand: Prisma.brandCreateInput = {
id: brandId,
name: payload.name,
website: payload.website,
slug: payload.slug,
image: { connect: { id: payload.image_id } },
brand_industry: { createMany: { data: industries } },
};
When i build this object, there is a chance that payload.image_id will be undefined which the prisma client accepts.
example:
{
id: '9f77cc53-732b-4c1d-bdca-b14f4cbc7643',
name: 'test',
website: 'test',
slug: 'test',
image: { connect: { id: undefined } },
brand_industry: { createMany: { data: [Array] } }
}
const createBrand = await PrismaService.brand.create({
data: newBrand,
});
However the query fails because image_id is undefined then. Kind of wish it would support if its undefined to insert null. WIthout me having to explicitly check.
---
looks like i can't set it to null excplicity even if i wanted to because the types don't allign.
image: { connect: { id: payload.image_id ? payload.image_id : null } },
Type 'null' is not assignable to type 'string | undefined'.
export type imageWhereUniqueInput = {
id?: string
}
Seems pretty awkward to me, am i doing something wrong? because i believe now i have to do the check before adding the newBrand.image fieldKay Khan
06/20/2022, 10:58 AMconst newBrand: Prisma.brandCreateInput = {
id: brandId,
name: payload.name,
website: payload.website,
slug: payload.slug,
brand_industry: { createMany: { data: industries } },
};
if (payload.image_id) {
newBrand.image = { connect: { id: payload.image_id } };
}
const createBrand = await PrismaService.brand.create({
data: newBrand,
});
Nurul
06/21/2022, 2:20 PMconnect
because as you mentioned the query is going to fail.
connect
connects a record to an existing related record by specifying an ID or unique identifier and if the related record does not exist, Prisma Client throws an exception.
Could you open an issue to describe this scenario, I don’t think we should allow undefined or null if the query is going to fail eventually.
You solution that you mentioned looks good as of now!Kay Khan
06/21/2022, 2:45 PMimage: { connect: { id: payload.image_id ? payload.image_id : null } },
but i can't because
export type imageWhereUniqueInput = {
id?: string
}
Nurul
06/22/2022, 11:12 AM