balderdashy / sails

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

Unable to access `columnName` and other attribute properties in sails model #7099

Open bhargavkn opened 3 years ago

bhargavkn commented 3 years ago

I'm unable to access the columName property from a Model's attributes in sails from other places in the code.

Here's an example illustrating this:

  1. Model
    
    // User.js -- model

module.exports = { tableName: 'users', attributes: { id: { type: 'string', columnType: 'varchar(40)', autoIncrement: true, columnName: 'user_id' } } };


2. Controller
```javascript
// UserController.js -- controller

module.exports = {
  execute(req, res) {
    console.log(User.attributes);
  }
};

/**
 * The above code prints:
 * {
 *   id: {
 *     type: 'string',
 *     autoMigrations: { autoIncrement: true, columnType: 'varchar(40)', unique: true },
 *     required: false
 *  }
 * }
 * (Notice the missing 'columnName' property)
 */
  1. Global Models Config
    // config/model.js
    module.exports.models = {
    migrate: 'safe'
    };

Can someone help me in understanding what I'm missing here and how I can go about accessing the columnName property from the model?

I felt it'd be easier to use the columnName property from the model and treating it as the source of truth instead of having to declare constants elsewhere (when creating a custom query, for example) that store the column names -- which is exactly what I'm trying to do.

For anyone who wants to debug this, I've put up the example sails application here: https://github.com/bhargavkn/sails-sample

Node version: v14.15.4: Sails version: 1.4.0 (sails): ORM hook version: 2.1.1 (sails-hook-orm): Sockets hook version: NA (sails-hook-sockets): Organics hook version: NA (sails-hook-organics): Grunt hook version: NA (sails-hook-grunt): Uploads hook version: NA (sails-hook-uploads): DB adapter & version: 1.0.1 (e.g. sails-mysql@5.55.5): Skipper adapter & versionL: NA (e.g. skipper-s3@5.55.5):


sailsbot commented 3 years ago

@bhargavkn 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.

eashaw commented 3 years ago

Hi @bhargavkn, you can access the columnName of attributes by calling [Model].schema.[attribute].columnName. For your example, the way to access the columnName of the id attribute would be User.schema.id.columnName.

bhargavkn commented 3 years ago

@eashaw That worked, thanks!

Please feel free to close this issue.

PS: Apologies for the late reply here.