Skip to content

upsert overwrites data with default values on update#3994

Merged
janmeier merged 2 commits intosequelize:masterfrom
FredKSchott:upsert-no-defaults
Jun 24, 2015
Merged

upsert overwrites data with default values on update#3994
janmeier merged 2 commits intosequelize:masterfrom
FredKSchott:upsert-no-defaults

Conversation

@FredKSchott
Copy link
Copy Markdown
Contributor

Currently, a defaultValue set on the model will be used in every upsert operation. This is fine for when upsert INSERTs, since that is when default values are used/needed. However, upsert is also using defaultValue on INSERT, which is causing data to be overwritten and lost by the default values.

I've included a test that is currently broken by this bug, I haven't had time to look into a fix yet. EDIT: fix also added

/cc @mickhansen @janmeier (from the last upsert issue we worked on)

@FredKSchott
Copy link
Copy Markdown
Contributor Author

I've updated the PR with a proposed fix. updateValues are currently created out of the full INSERT data object, and has no way of knowing which fields are defaults and which should be changed. This PR moves this creation up closer to the model where default value and change information is still known.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use sinon fake timers here, to artifically advance time http://sinonjs.org/docs/#clock

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this situation I think we need the actual delay so that sequelize can internally get the proper Date() times on both create & upsert. I've used sinon to fake timers before, but iirc sinon can't modify the actual system time which is what we need here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use new Date() internally, and that is exactly what fake timers override :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must be misremembering then, I remember a lot of trouble last time I used these for anything other than extending timers. I'll experiment a bit tomorrow and see if I can get it to work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janmeier definitely misremembered, this worked perfectly!

@janmeier janmeier self-assigned this Jun 24, 2015
@janmeier
Copy link
Copy Markdown
Member

LGTM - just a single comment

lib/model.js Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely style: mind using leading commas with a single var statement?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fasho

@FredKSchott
Copy link
Copy Markdown
Contributor Author

@janmeier @mickhansen comments addressed

janmeier added a commit that referenced this pull request Jun 24, 2015
upsert overwrites data with default values on update
@janmeier janmeier merged commit 3d0af74 into sequelize:master Jun 24, 2015
@janmeier
Copy link
Copy Markdown
Member

Thanks @FredKSchott ! :)

@FredKSchott
Copy link
Copy Markdown
Contributor Author

Woot! thanks for the quick responses :)
Is there any way we could push a patch in the next few days to get this out? It sucks to wait 2-3 weeks for every change to get upsert working.

@janmeier
Copy link
Copy Markdown
Member

If you bug @mickhansen then probably yes :)

In the meantime you can point your package.json to sequelize/sequelize#3d0af74947b71b2523b7cff873963c506e5a7a75

@FredKSchott
Copy link
Copy Markdown
Contributor Author

Yea good point, I'll point for now. Thanks @janmeier!

@FredKSchott FredKSchott deleted the upsert-no-defaults branch June 24, 2015 20:49
mickhansen added a commit that referenced this pull request Jun 25, 2015
@mickhansen
Copy link
Copy Markdown
Contributor

Released in 3.3.2

@FredKSchott
Copy link
Copy Markdown
Contributor Author

Thanks @mickhansen!

@AlbertoLunaPe
Copy link
Copy Markdown

Hi, since this release, the upsert function is not working properly.
I am trying to upsert an entry with 2 unique fields. Those fields are included in order entries so when I execute the upsert there is a MySQL error "error: uncaughtException: Cannot read property 'uniqueKeys' of undefined" and it is no catched with the .catch method so the app goes down.

@janmeier
Copy link
Copy Markdown
Member

janmeier commented Jul 3, 2015

@telpalbrox please open a new thread with some code that shows the error

@ciberyo16
Copy link
Copy Markdown

thank you @janmeier I opened it here #4059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants