Refresh model attributes after a save operation#2012
Merged
Conversation
- This makes them more accurate when working with MySQL that doesn't support millisecond timestamp columns by default when used with Knex.
- This will only be used if it's present and the unparsed idAttribute is not.
- Previously any method that called _doFetch() would get the previous attributes reset, but we don't want to do this when calling refresh from the save method.
- With the recent change of refreshing the model after save both the `this` and `updatedModel` were the same thing. - This change also makes all the "after" save events more consistent since the arguments are all the same. - Finally, the previous values returned as the second argument just weren't very useful and the id of the inserted model is available in the updated model, so no data is lost.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduction
Modify
Model#save()so that it returns a fresh copy of the model from the database after a successful save.Motivation
It is often necessary to get all the attributes from the database immediately after a save. The current behavior is also not very intuitive, where after a save some attributes will be set but not others. For example, the timestamp columns might be missing entirely if the user didn't set them manually (#507).
This should lead to a much better experience when working with models, since the model will always have the most up-to-date attributes without needing to manually call
Model#refresh.Fixes #507. Closes #1665.
Proposed solution
This will call
Model#refreshafter a successful save on databases that do not support theRETURNINGstatement. On those that do a more efficient singleINSERTORUPDATEquery will be generated allowing this feature to have negligible performance impact in that case.Note that the after save event signatures were changed to make them more consistent. Namely the second argument was removed since it wasn't very useful in any case (for update events it was always
1and for inserts it was[1]or whatever the new inserted id was). Nonetheless this is a small breaking change, so a migration guide has been added.Current PR Issues
Requires two queries on database that do not support
RETURNING, but there's no way around that.