Repository navigation
Closed
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Description
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')
Reacted by Idris Mokhtarzada, Punit, Vinh Pham and grutty
Metadata
Metadata
Assignees
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Activity
wkkwok commentedon Mar 14, 2016
+1
Virtaul attributes seems to be added last, can't remove them even with afterFind Hook.
wkkwok commentedon Mar 15, 2016
I finally figure it out myself. It seems that custom getter are called only in
Instance.getafter 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.zuck commentedon Sep 5, 2016
In my opinion this is a bug that must be fixed.
janmeier commentedon Sep 5, 2016
@zuck Contributions welcome :) https://github.com/sequelize/sequelize/blob/master/CONTRIBUTING.md
Fixed sequelize#5566 , deleted double for-in which always include vir…
Fixed sequelize#5566 , deleted double for-in which always include vir…
nstuyvesant commentedon Mar 19, 2017
Would it make sense to update the docs for now to qualify
works but only if baz is not type: DataTypes.VIRTUAL?
javaguy commentedon May 5, 2017
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.
huynhj93 commentedon May 25, 2017
@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.
stale commentedon Jul 24, 2017
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 🙂
scottbeacher commentedon Sep 22, 2017
This is still an issue.
vinhboy commentedon Sep 29, 2017
@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
You should submit a PR if you have time to work on one. Thanks.
Undre4m commentedon Dec 1, 2017
+1
onerciller commentedon Jan 9, 2018
+1
Fix for excluding virtual attributes from queries, adapted from seque…
CherryChann commentedon Feb 6, 2018
This is still issue and I cannot exclude the attributes with getter function..:(
chemic commentedon May 11, 2018
Is it still like that, or there's a way to filter VIRTUAL out somehow?
Thanks!
smithaitufe commentedon Aug 23, 2018
So basically, how are virtual field ignored?
elwin1234 commentedon Feb 18, 2019
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 usingfindByPk,findAll,find*functions?artfauq commentedon Feb 12, 2020
Any update on this issue ? Still not solved in release
v5.21.3...