Broken upsert query with defaultValue from `Sequelize.literal('uuid_generate_v4()')`
See original GitHub issueWhat are you doing?
When I define a model that has an uuid field that uses Sequelize.literal('uuid_generate_v4()')
as its defaultValue
sequelize.define(
'thing',
id: {
type: Sequelize.UUID,
field: 'id',
primaryKey: true,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
},
name: {
type: Sequelize.TEXT,
field: 'name',
unique: true,
},
nickname: {
type: Sequelize.TEXT,
field: 'nickname',
}
}
And run upsert on the model thing.upsert({ name: 'john', nickname: 'johnjohn' })
the resulting WHERE clause of the “update query” portion turns out like this:
--...
WHERE (uuid_generate_v4() OR ("name" = 'foo', "nickname" = 'johnjohn'))
--...
Which results in an error of [SequelizeDatabaseError: argument of OR must be type boolean, not type uuid]
I would expect the generated SQL to be
--...
WHERE ("id" = uuid_generate_v4() OR ("name" = 'foo', "nickname" = 'johnjohn'))
--...
I did some tracing and found that in models.ts#L2465
The instance.where()
returns { id: Literal { val: 'uuid_generate_v4()' } }
So far so good
It eventually hits this line in QueryGenerator.whereItemQuery
where this.handleSequelizeMethod
just returns the unwrapped Literal’s value ('uuid_generate_v4()'
)
https://github.com/sequelize/sequelize/blob/b6c9117f279d3df19565797ae3fc1be0a1a5eda9/lib/dialects/abstract/query-generator.js#L1970-L1972
I monkeypatched a fix with this and it worked
if (value instanceof Utils.SequelizeMethod && !(key !== undefined && value instanceof Utils.Fn)) {
- return this.handleSequelizeMethod(value);
+ this._joinKeyValue(key, this.handleSequelizeMethod(value), this.OperatorMap[Op.eq], options.prefix)
}
This resulted in the generated SQL to be
--...
WHERE ("id" = uuid_generate_v4() OR ("name" = 'foo', "nickname" = 'johnjohn'))
--...
Dialect: postgres Database version: 11.2 Sequelize version: 4.42 Tested with latest release: No (If yes, specify that version)
Note : Your issue may be ignored OR closed by maintainers if it’s not tested against latest version OR does not follow issue template.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:14 (6 by maintainers)
I’m having the same issue! At the moment doing things manually but would be good to have a working
upsert
method.This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the “stale” label. 🙂