question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

virtual attributes are always return by model

See original GitHub issue
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’)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
arthurfauqcommented, Feb 12, 2020

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

7reactions
scottbeachercommented, Sep 22, 2017

This is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Virtual attributes in rails model - ruby - Stack Overflow
Usually virtual attributes are used when some attribute is not stored directly in the DB, but can be converted from and to an...
Read more >
Can I modify the display value of a virtual attribute? | Clarity
I am adding a couple virtual attributes to display data from the master object (project) into a sub-object (status reports).
Read more >
Column Aliases & Virtual Attributes
Is it possible to select a column alias and have it automatically assigned to the corresponding model object attribute. I'm assuming
Read more >
Mongoose Virtuals
Virtuals are properties on Mongoose documents. If you use the lean option, that means your queries return POJOs rather than full Mongoose documents....
Read more >
Understanding Virtual Attributes and get/set methods | Wiki
With this getter function defined, $model->fullname automatically calls the function and returns the value as if it were a real attribute: this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found