Skip to content

virtual attributes are always return by model #5566

Closed

Description

@p2227
var pinyin = require('pinyin');

module.exports = function(sequelize, DataTypes) {
  var moneyType = sequelize.options.dialect === 'mssql' ? 'MONEY' : DataTypes.INTEGER;
  return sequelize.define('Pt', {
    usertype:{
      type:DataTypes.VIRTUAL,
      get(){
        return '0'
      }
    },
    pinyin:{
      type:DataTypes.VIRTUAL,
      scopes:['user'],
      get(){
        return pinyin(this.getDataValue('TX_Name'),{
          segment:false,
          style:pinyin.STYLE_NORMAL
        }).join('')
      }
    },
    ID_User: {
      primaryKey: true,
      type: DataTypes.STRING,
      allowNull: false
    },
    TX_Name: {
      type: DataTypes.STRING,
      allowNull: true
    },
    DT_Birth: {
      type: DataTypes.DATE,
      get:function(){
        var d = this.getDataValue('DT_Birth')
        return d ? d.toLocaleString() : d; 
      },
      allowNull: true
    },
    TX_IC: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_IMG: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Location: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Signature: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Username: {
      type: DataTypes.STRING,
      allowNull: false
    },
    TX_Password: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Contact: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_QQ: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_WeChat: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_QR: {
      type: DataTypes.STRING,
      allowNull: true
    },
    DT_Delivery: {
      type: DataTypes.DATE,
      get:function(){
        var d = this.getDataValue('DT_Delivery')
        return d ? d.toLocaleString() : d; 
      },
      allowNull: true
    },
    TX_Name2: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Contact2: {
      type: DataTypes.STRING,
      allowNull: true
    },
    MN_Credit: {
      type: moneyType,
      allowNull: true
    },
    MN_Deposit: {
      type: moneyType,
      allowNull: true
    },
  }, {
    defaultScope: {
      attributes: { exclude: ['TX_Password'] }
    },
    scopes:{
      'user':{
        attributes:['ID_User','TX_Name','DT_Birth','TX_IC','TX_IMG','TX_Location','TX_Signature','TX_Username','TX_Password','TX_QR','TX_Contact','TX_QQ','TX_WeChat']
      }
    },
    updatedAt:'DTTM_Updated',
    createdAt:'DTTM_Created',
    tableName: 'T_Pt',
    freezeTableName: true
  });
};

the attributes usertype and pinyin are always return where i findOne/findAll event i use model.scope('xxx')

Activity

  1. wkkwok commented on Mar 14, 2016

    @wkkwok

    +1
    Virtaul attributes seems to be added last, can't remove them even with afterFind Hook.

  2. wkkwok commented on Mar 15, 2016

    @wkkwok
    this.addHook('afterFind', 'removeActivationCode', async function(users, options) {
        if (!users)
            return;
        if (users.constructor != Array)
            users = [users];
        for (var i = 0; i < users.length; i++) {
            if (users[i]._hasCustomGetters) {
                users[i]._customGetters = _.pick(users[i]._customGetters, options.attributes)
            }
        }
    });
    

    I finally figure it out myself. It seems that custom getter are called only inInstance.get after the all options are parsed. Therefore,find(options.attributes[]) will not affect the output. I am not sure if this is the intended outcome or a bug.

  3. zuck commented on Sep 5, 2016

    @zuck

    In my opinion this is a bug that must be fixed.

  4. added 2 commits that reference this issue on Feb 28, 2017
    29e3d56
    c44620c
  5. nstuyvesant commented on Mar 19, 2017

    @nstuyvesant

    Would it make sense to update the docs for now to qualify

    Model.findAll({
      attributes: { exclude: ['baz'] }
    });
    

    works but only if baz is not type: DataTypes.VIRTUAL?

  6. javaguy commented on May 5, 2017

    @javaguy

    I looked into this issue as well since I'm facing the same problem. I believe this can be fixed in Instance.get which indiscriminately calls all the custom getters. Instead there should probably be a check to see if the getter corresponds to an included attribute.

    https://github.com/sequelize/sequelize/blob/v3.30.4/lib/instance.js#L207

    @janmeier I'd be happy to submit a PR for this if you can confirm I'm on the right track.

  7. huynhj93 commented on May 25, 2017

    @huynhj93

    @wkkwok Is that fix now deprecated? I tried implementing it, but options.attributes seems to exclude the virtual attributes. Thus, now your model._customGetters will be an empty object.

  8. stale commented on Jul 24, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  9. scottbeacher commented on Sep 22, 2017

    @scottbeacher

    This is still an issue.

  10. vinhboy commented on Sep 29, 2017

    @vinhboy

    @javaguy I was able to fix it in my environment by patching that file with

    https://github.com/sequelize/sequelize/blob/v3.30.4/lib/instance.js#L207

        if (this._hasCustomGetters) {
          for (_key in this._customGetters) {
            // Don't include VIRTUAL fields that are not specified in $options.attributes
            if (this.$options.attributes && !this.$options.attributes.includes(_key)) {
              continue;
            }
            if (this._customGetters.hasOwnProperty(_key)) {
              values[_key] = this.get(_key, options);
            }
          }
        }

    You should submit a PR if you have time to work on one. Thanks.

  11. Undre4m commented on Dec 1, 2017

    @Undre4m

    +1

  12. onerciller commented on Jan 9, 2018

    @onerciller

    +1

  13. added
    type: bug
    on Jan 9, 2018
  14. CherryChann commented on Feb 6, 2018

    @CherryChann

    This is still issue and I cannot exclude the attributes with getter function..:(

  15. chemic commented on May 11, 2018

    @chemic

    Is it still like that, or there's a way to filter VIRTUAL out somehow?
    Thanks!

  16. smithaitufe commented on Aug 23, 2018

    @smithaitufe

    So basically, how are virtual field ignored?

  17. elwin1234 commented on Feb 18, 2019

    @elwin1234

    This issue is still not solved in current stable release v4.42.0, right? Any virtual property, whether it has a custom get() function or not, is never correctly excluded when using findByPk, findAll, find* functions?

  18. artfauq commented on Feb 12, 2020

    @artfauq

    Any update on this issue ? Still not solved in release v5.21.3...

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions