-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bulkCreate with updateOnDuplicate updates the record (updatedAt) even if it is not changed #17247
Comments
In MySQL, this query is done using -- assumes "a" is unique
INSERT INTO daily_events (a, b, updated_at)
VALUES (1, 2, '2024-04-04 04:04:04') AS new
ON DUPLICATE KEY UPDATE
b = new.b,
updated_at = IF(a <> new.a, new.updated_at, updated_at); Similarly, |
Interesting, how can we do it with Sequelize? Does Sequelize support it? Looks like Sequelize we can only provide the array of the fields, please check here updateOnDuplicate?: Array<keyof TAttributes>; |
Nothing clean as far as I know. Maybe you'll have some change with |
Issue Creation Checklist
Bug Description
I am using
bulkCreate
to create/update the records. If the records are already existing then they should get updated only if the required field has changed and also should automatically update the updatedAt timestamp.The issue I am facing is I have to provide the
updatedAt
field inupdateOnDuplicate
then only it updates the timestamp, and if I do so, then it updates the timestamp even if the required column is not changed, if I don't provide theupdatedAt
field in theupdateOnDuplicate
options then it only updates the required field and doesn't update the timestamp.Reproducible Example
Let's say that my table structure is:
Table Foo (a:int unique, b: string, createdAt: datetime, updatedAt: datetime)
and it has two records
if I use bulkCreate operation on table foo:
then it updates updatedAt for both records even when there was no change in second record (with b = 'two').
And if I use bulkCreate operation like the following
then it updates value of
b
for first record but doesn't update the timestampupdatedAt
for the first record.What do you expect to happen?
Ideally, the updatedAt timestamp should be handled by sequelize package, and it should update it only if there is any change in field
b
.What is actually happening?
Environment
Would you be willing to resolve this issue by submitting a Pull Request?
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
The text was updated successfully, but these errors were encountered: