question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ID when use upsert, connectOrCreate and things like that

See original GitHub issue

I was really struggling yesterday with an update query to a table with many different relations. I spent hours to understand the errors and find the solution. So yesterday i gave up and i tried it recently and found a solution. But the solution is not that clean as i wanted. Maybe its just my opinion.

Here ist what i wanted to do.


this.prisma.blogs.update({
            where: {
                id: req.body.id
            },
            data: {
                title: req.body.title,
                text: req.body.text,
                images: {
                    connect: { id: req.body.images_id }
                },
                embed: req.body.embed,
                embedType: req.body.embedType,
                categories: {
                    connect: { id: req.body.categories_id }
                },
                sources: {
                    connectOrCreate: req.body.sources.map((source: any) => { return {
                        where: { id: source.id ? source.id : 0 },
                        create: { name: source.name, url: source.url }
                    }})
                },
                blogs_has_tags: {
                    upsert: req.body.tags.map((tag: any) => { return {
                        where: {
                            blogs_id_tags_id: {
                                blogs_id: req.body.id,
                                tags_id: tag.id ? tag.id : 0
                            }
                        },
                        create: {
                            tags: {
                                connectOrCreate: {
                                    where: { id: tag.id ? tag.id : 0 },
                                    create: { name: tag.name }
                                }
                            }
                        },
                        update: {
                            tags: {
                                connectOrCreate: {
                                    where: { id: tag.id ? tag.id : 0 },
                                    create: { name: tag.name }
                                }
                            }
                        }
                    }})
                },
                updatedAt: new Date()
            },
            include: {
                categories: true,
                images: true,
                blogs_has_tags: {
                    include: {
                        tags: true
                    }
                },
                users: {
                    select: {
                        realName: true
                    }
                },
                sources: true
            }
        })

Take a look at the where definitions of the relations. I pass an array of elements with maybe new elements and i have to check if the new element in the relation table exists or not, when not create a new record for it. In the documentation i read that you are focused to use the where definition, what is no problem at the moment. Because i have to check existence. But what me bothered is that you are focused to pass a value. So for id its an integer. The problem with new elements, that you want to add to the table, is that they dont have an id yet. Yesterday i got errors in my query at the point where i wanted to check the new element if its exists in the table, because the id field was undefined. So in the where definition i have to check if my id field exists, when not then pass a fake id, in my example just 0.

So my request would be, when you work with relations and you use things like connectOrCreate, then it would be very cool, when the id check in the where definition would accept undefined. So its not needed to pass a fake value. I hope you can understand what i mean with this.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
pantharshit00commented, Dec 24, 2020

Related: https://github.com/prisma/prisma/issues/4648

Maybe we should make both operations optional here.

2reactions
vuki656commented, Feb 17, 2022

This also creates a bad user experience when using uuids as IDs. Since you can’t pass undefined or null but instead have to generate a fake uuid.

I had to do this:

private create = (params?: ParamsType): Prisma.OptionCreateInput => {
    return {
        ...this.generateOne(),
        products: {
            connectOrCreate: {
                create: {
                    ...this.productFactory.generateOne(),
                },
                where: {
                    id: params?.existing?.productId ?? faker.datatype.uuid(),
                },
            },
        },
    }
}

What I would expect is if I pass null/undefined in the where clause, it would go to the create clause resulting in much cleaner code.

private create = (params?: ParamsType): Prisma.OptionCreateInput => {
    return {
        ...this.generateOne(),
        products: {
            connectOrCreate: {
                create: {
                    ...this.productFactory.generateOne(),
                },
                where: {
                    id: params?.existing?.productId,
                },
            },
        },
    }
} 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Relation queries (Concepts) - Prisma
A key feature of Prisma Client is the ability to query relations between two or more models. Relation queries include: Nested reads (sometimes...
Read more >
How to use connectOrCreate with many to many in prisma
Here's an example from the Prisma Docs that shows how to use connectOrCreate with multiple records. This is what your create query should...
Read more >
Mule Salesforce connector Create Juntion Object using ...
I would like to upsert records into Junction Object C without having to know the Salesforce Ids of Object A and Object B....
Read more >
ConnectOrCreate - Neo4j - 56207
By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies. Accept · Reject....
Read more >
IcM Connector "Create or update an IcM incident" r...
When we use the "Create or update an IcM incdent" action, the action always returns a 500 "Internal Server Error" like this with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found