romeerez / orchid-orm

Orchid ORM
https://orchid-orm.netlify.app/
MIT License
499 stars 14 forks source link

[1.38.2] `upsert` is broken #438

Open mordechaim opened 1 day ago

mordechaim commented 1 day ago

In the latest OrchidORM 1.38.2 upsert is broken for me. Trying to upsert in my code throws this error:

node_modules\pqb\dist\index.mjs (4695:1) @ then
 ⨯ Internal error: error: WITH query "c" does not have a RETURNING clause

I tried to reproduce it on a clean project but it works in isolation so I'm not sure what exactly triggers it. But I do see a recent commit that touched upsert https://github.com/romeerez/orchid-orm/commit/2b3ae9b0c7c4334bbb850b78c94a8e82690e6de7.

My code works perfectly in Orchid 1.38.1

romeerez commented 23 hours ago

Does your code has select, get, pluck, rows, something like that? Because the error suggests that the bug is related to RETURNING, i.e what you're selecting from the upsert. Maybe this is related to computed columns, give me a hint.

mordechaim commented 23 hours ago

This is the query verbatim

await db.facilityRole.findBy({ facilityId, roleId }).upsert({
        data: {
            billRate,
            payRate
        },
        create: {
            roleId,
            facilityId,
            billRate,
            payRate
        }
    })

No computed fields on this table, just a simple join table with 4 columns roleId, facilityId, billRate and payRate plus id and timestamps.

The role and facility columns have foreign keys, and unique index on them.

romeerez commented 23 hours ago

Looks like it's not meant to have a returning at all and that's a direction to look for the bug.

I'll fix on the next occasion, and for now downgrade the version temporary.

mordechaim commented 22 hours ago

One more detail, it only happens if the record is not found and a new is created