hi folks, need your help. I have to store multiple...
# orm-help
g
hi folks, need your help. I have to store multiple country name record on the same date but this query only allows to have one per day how to solve it?
Copy code
const upsert = await prisma.analytics.upsert({
        where: { date: new Date(body.date) },
        update: {
            viewers: body.viewers,
        },
        create: {
            date: new Date(body.date),
            country: body.country,
            viewers: body.viewers,
        }
    })
    res.status(204).json(upsert)
s
Sounds like adding
country
to the where clause should do the trick:
Copy code
const upsert = await prisma.analytics.upsert({
        where: { date: new Date(body.date), country: body.country },
        update: {
            viewers: body.viewers,
        },
        create: {
            date: new Date(body.date),
            country: body.country,
            viewers: body.viewers,
        }
    })
    res.status(204).json(upsert)
g
nope, you can only put one argument. also country name can be repeated, but args must be unique too
s
if you set a unique constraint to date and country, then will you be able to use
date
and
country
in the where clause?
g
unfortunately no you can't
s
Hey, I've been playing around with this. If you add the unique contraint
@@unique([date, country])
and regenerate the types, you will get a new field that you can use in the where clause:
date_country
Then you will be able to do:
Copy code
const upsert = await prisma.analytics.upsert({
        where: { date_country: { date: new Date(body.date), country: body.country } },
        update: {
            viewers: body.viewers,
        },
        create: {
            date: new Date(body.date),
            country: body.country,
            viewers: body.viewers,
        }
    })
    res.status(204).json(upsert)
g
pog, O my god it works 😊 date_country: it's slightly weird but it works. thank you Sebastian 🎉
🙌 2