balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Waterline - columnName not taken into account for create request #7106

Open bsoufflet opened 3 years ago

bsoufflet commented 3 years ago

Node version: v10.23.3 Sails version (sails): 1.4.0 ORM hook version (sails-hook-orm): 3.0.0 Sockets hook version (sails-hook-sockets): 2.0.0 Organics hook version (sails-hook-organics): Nop Grunt hook version (sails-hook-grunt): 4.0.1 Uploads hook version (sails-hook-uploads): Nop DB adapter & version (e.g. sails-mysql@5.55.5): sails-postgresql@2.0.0 Skipper adapter & version (e.g. skipper-s3@5.55.5): Nop


I have a model Tcave where an attribute is described like this:

idAuthor: {
      columnName: 'id_author',
      type: 'number',
    },

When I try to run create on this I get the following error: Unexpected error from database adapter: column "idAuthor" of relation "t_cave" does not exist

This is normal because the table t_cave do not has a column idAuthor but it has the column id_author

If I change the attribute to this, it is working fine:

id_author: {
      columnName: 'id_author',
      type: 'number',
    },

Is this related to this question ? https://stackoverflow.com/questions/37516556/sails-js-one-to-many-relationship-with-custom-column-names

sailsbot commented 3 years ago

@bsoufflet 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:

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.

gm0re commented 3 years ago

Having the same issue over here: https://github.com/balderdashy/sails/issues/7105 :)

gm0re commented 3 years ago

Take a look at my comment over here: https://github.com/balderdashy/sails/issues/7105#issuecomment-784240108

This will unblock you and it's pretty straightforward.

eashaw commented 3 years ago

Hey @bsoufflet, I was taking a look at this, and I wasn't able to reproduce the issue you're running into. If you create a repo with a fresh Sails app that reproduces this, I'd be happy to look at it. Thanks!

vmarseguerra commented 1 year ago

I believe I found our issue here. It is because in the model there is a numeric attribute named length

The function that converts the model attributes to the database column name: transformer.serializeValues() use _.each() However _.each() expects an array-like collection An object with a length property will be interpreted as an array instead of an object like expected here More detail here: https://github.com/lodash/lodash/issues/277

Using _.forOwn() like recommended solves the issue

https://github.com/balderdashy/waterline/blob/master/lib/waterline/utils/system/transformer-builder.js#L160