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.

Broken upsert query with defaultValue from `Sequelize.literal('uuid_generate_v4()')`

See original GitHub issue

What 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

https://github.com/sequelize/sequelize/blob/b6c9117f279d3df19565797ae3fc1be0a1a5eda9/lib/dialects/abstract/query-generator.js#L2092-L2094

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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cminardicommented, Jul 22, 2019

I’m having the same issue! At the moment doing things manually but would be good to have a working upsert method.

0reactions
github-actions[bot]commented, Nov 14, 2021

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. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

sequileze is not taking UUIDV4 as default value - Stack Overflow
js process with a non-zero exit code. Explaination. when i write defaultValue : sequelize.UUIDV4 it is not still taking it as default value...
Read more >
uuid_generate_v4 no function matches - You.com | The search ...
I expected no errors. There are no errors while running locally. What is actually happening? function uuidgeneratev4() doesn't exist! Dialect: postgres Dialect ...
Read more >
Postgres autogenerated UUIDs with Sequelize
DataTypes.UUID, defaultValue: Sequelize.UUIDV4, // I expected this set the column default }, email: { type: Sequelize.
Read more >
Sequelize setting default value using uuid_gene...anycodings
Sequelize setting default value using uuid_generate_v4 gives syntax error I ... Error: Invalid value Literal { val: 'uuid_generate_v4()' }.
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