Skip to content
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

Model.update() converts object keys passed in set() from attributes name to SQL columnName #7165

Open
himankpathak opened this issue Aug 17, 2021 · 7 comments

Comments

@himankpathak
Copy link

himankpathak commented Aug 17, 2021

Node version: 16.6.1
Sails version (sails): 1.4.4
ORM hook version (sails-hook-orm): 2.1.1
Sockets hook version (sails-hook-sockets): 1.5.5
Organics hook version (sails-hook-organics): NA
Grunt hook version (sails-hook-grunt): 1.0.8
Uploads hook version (sails-hook-uploads): NA
DB adapter & version : sails-postgresql@2.0.0
Skipper adapter & version : skipper@0.9.0


Issue

When calling Model.update().set(), sails waterline is modifying the object which is passed to the set function.
Camel case keys are attribute names in the sails model and in SQL the column names are in snake case.
Camel case is converted to snake case after the update call.

const newMessage = {
id: 123
sessionId: 1234,
messageBody: 'This is a test message'
...
}

await Promise.all([
...
Model.update({ id: newMessage.id }).set(newMessage)
])

console.log(newMessage)
/*
{
id: 123
session_id: 1234,
message_body: 'This is a test message'
...
}
*/

@sailsbot
Copy link

@himankpathak Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

  • look for a workaround. (Even if it's just temporary, sharing your solution can save someone else a lot of time and effort.)
  • tell us why this issue is important to you and your team. What are you trying to accomplish? (Submissions with a little bit of human context tend to be easier to understand and faster to resolve.)
  • make sure you've provided clear instructions on how to reproduce the bug from a clean install.
  • double-check that you've provided all of the requested version and dependency information. (Some of this info might seem irrelevant at first, like which database adapter you're using, but we ask that you include it anyway. Oftentimes an issue is caused by a confluence of unexpected factors, and it can save everybody a ton of time to know all the details up front.)
  • read the code of conduct.
  • if appropriate, ask your business to sponsor your issue. (Open source is our passion, and our core maintainers volunteer many of their nights and weekends working on Sails. But you only get so many nights and weekends in life, and stuff gets done a lot faster when you can work on it during normal daylight hours.)
  • let us know if you are using a 3rd party plugin; whether that's a database adapter, a non-standard view engine, or any other dependency maintained by someone other than our core team. (Besides the name of the 3rd party package, it helps to include the exact version you're using. If you're unsure, check out this list of all the core packages we maintain.)

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

@eashaw
Copy link
Member

eashaw commented Sep 3, 2021

Hey @himankpathak, I wasn't able to reproduce this behavior you're seeing. Would you be willing to create a github repo with a minimal sails app that reproduces this issue?

@eashaw eashaw added the repro please Could you reproduce this in a repository for us? label Sep 3, 2021
@himankpathak
Copy link
Author

himankpathak commented Nov 11, 2021

Hey @eashaw ,
I have created a repo on which this bug is very easily reproducible. https://github.com/himankpathak/sails-obj-bug

Migrations for the app are mentioned in Readme.

Output:

Current value: {
  createdAt: 2021-11-11T07:22:31.297Z,
  updatedAt: 2021-11-11T07:22:31.297Z,
  id: 1,
  user: 2,
  action: 'This is test data'
}
After passing the object to update function
Modified value: {
  id: 1,
  created_at: 2021-11-11T07:22:31.297Z,
  updated_at: 2021-11-11T07:22:31.297Z,
  user_id: 2,
  action_name: 'This is test data'
}
Current value: {
  createdAt: 2021-11-11T07:22:53.407Z,
  updatedAt: 2021-11-11T07:22:53.407Z,
  user: 4,
  action: 'This_is_test_data'
}
After passing the object to create function
Modified value: {
  created_at: 2021-11-11T07:22:53.407Z,
  updated_at: 2021-11-11T07:22:53.407Z,
  user_id: 4,
  action_name: 'This_is_test_data'
}

@sailsbot sailsbot removed the repro please Could you reproduce this in a repository for us? label Nov 11, 2021
@eashaw
Copy link
Member

eashaw commented Feb 18, 2022

Hi @himankpathak,
I took a look at your repo and I just noticed that you are logging the same object passed into Model.update().
Mutation on objects passed into Model.update() is expected behavior.

To get the updated value, you can set a variable using model.update() e.g., let foo = await User.update({id: 1}).fetch() (Note that .fetch() is required to return the updated record when using the .update() method.)

You can read more about .update() in our docs.

@himankpathak
Copy link
Author

Hey @eashaw,
I found this to be an unexpected behavior as this issue does not occur in v0.12.
There is no mention of this behavior either in method docs or in migration to v1 docs.

It does make sense to use .fetch() method to get the updated record. But the valuesToSet passed to an ORM in some of my use cases are used to drive additional logic.
Also to note that I have never seen such behavior from other ORMs that I have worked with before.

@eashaw
Copy link
Member

eashaw commented Apr 29, 2022

Hi @himankpathak, I was looking at this with @mikermcneil. We realized that there was a gap in the upgrade guide here.

Here's some background on why this is happening:

Are you seeing this behavior on sails-postgresql@4.0.0?

@himankpathak
Copy link
Author

Thanks @eashaw for providing the background on this issue.
and yes, this behaviour is occurring with sails-postgresql@4.0.0.

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

No branches or pull requests

3 participants