I'm having an issue with using `[entity].create()`...
# prisma-client
k
I'm having an issue with using
[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:
Copy code
`image_id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
Copy code
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:
Copy code
{
  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.
Copy code
image: { connect: { id: payload.image_id ? payload.image_id : null } },
Copy code
Type 'null' is not assignable to type 'string | undefined'.
Copy code
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 field
1
solution im left with:
Copy code
const 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,
        });
n
Hey 👋 I am not 100% sure if we should allow passing undefined to
connect
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!
k
@Nurul i believe i should be able to do this given that the id is nullable in the table.
Copy code
image: { connect: { id: payload.image_id ? payload.image_id : null } },
but i can't because
Copy code
export type imageWhereUniqueInput = {
    id?: string
  }
n
To connect to an existing record (image in this case) that particular record needs to be identified uniquely, that could be the reason why null value isn’t allowed.