sequelize: queryInterface.upsert fails, reading 'primaryKeys' of undefined
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don’t fill in the requested information
- I have read the contribution guidelines
Bug Description
When using queryInterface.upsert in a seed file, records are not updated on conflict, but instead an error is thrown:
ERROR: Cannot read properties of undefined (reading 'primaryKeys')
Reproducible Example
Here is the link to the SSCCE for this issue: https://github.com/Jdmorrisett/sequelize-demo
- Apply all migrations
sequelize db:migrate
- Apply the first seed file (
20220804024548-my-seed-file
) and attempt to apply the issue seed file (20220804025436-issue-with-upsert
) and observe the issuesequelize db:seed --seed 20220804024548-my-seed-file.js 20220804025436-issue-with-upsert.js
let insertValues = [
{
first_name: "John",
last_name: "Doe",
bio: "I just updated my profile again",
createdAt: new Date(),
updatedAt: new Date(),
email: "johnDoe@test.com",
},
{
first_name: "Jane",
last_name: "Doe",
bio: "I am a new user to this application",
createdAt: new Date(),
updatedAt: new Date(),
email: "janeDoe@test.com",
},
];
// https://sequelize.org/api/v6/class/src/dialects/abstract/query-interface.js~queryinterface#instance-method-upsert
// ERROR: Cannot read properties of undefined (reading 'primaryKeys')
for (let values of insertValues)
await queryInterface.upsert(
"Users",
values,
{
bio: values.bio,
email: values.email,
updatedAt: values.updatedAt,
},
{ first_name: values.first_name, last_name: values.last_name }
);
},
What do you expect to happen?
I expect the bio, email, and updatedAt fields to be updated on the record for “John Doe” User in the database, and for User “Jane Doe” to be inserted.
What is actually happening?
The “John Doe” user isn’t updated and an error is thrown: ERROR: Cannot read properties of undefined (reading 'primaryKeys')
Environment
- Sequelize version: 6.21.3
- Node.js version: v16.14.0
- If TypeScript related: TypeScript version:
- Database & Version: PostgreSQL 13
- Connector library & Version: pg v8.7.3
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don’t have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don’t have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as “+1” will be removed.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 17 (8 by maintainers)
Thanks for the answer!
However there should be a few modifications with the workaround:
You should require/import the DataTypes from Sequelize first, because the Sequelize object doesn’t have these types on its own, but correct me if I’m wrong.
So the modified code should look like something like this:
This should work in plain JS (not tested). However if you have TS (like me), there will be some type mismatch between
QueryOptionsWithModel
andModelStatic
orModelCTor
- this one is deprecated. So for TS usage I don’t really see a proper workaround for this right now. 😦