不要怂,就是干,撸起袖子干!

Commit 6c1f15f1 by Ian Mathews Committed by Sushant

fix(model): injectDependentVirtualAttrs on included models (#11713)

1 parent 819e9ddb
...@@ -586,7 +586,7 @@ class Model { ...@@ -586,7 +586,7 @@ class Model {
if (include.attributes && !options.raw) { if (include.attributes && !options.raw) {
include.model._expandAttributes(include); include.model._expandAttributes(include);
include.originalAttributes = this._injectDependentVirtualAttributes(include.attributes); include.originalAttributes = include.model._injectDependentVirtualAttributes(include.attributes);
include = Utils.mapFinderOptions(include, include.model); include = Utils.mapFinderOptions(include, include.model);
......
...@@ -1423,7 +1423,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -1423,7 +1423,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
it('should pull in dependent fields for a VIRTUAL', function() { it('should pull in dependent fields for a VIRTUAL', function() {
const User = this.sequelize.define('User', { const User = this.sequelize.define('User', {
active: { active: {
type: new Sequelize.VIRTUAL(Sequelize.BOOLEAN, ['createdAt']), type: Sequelize.VIRTUAL(Sequelize.BOOLEAN, ['createdAt']),
get() { get() {
return this.get('createdAt') > Date.now() - 7 * 24 * 60 * 60 * 1000; return this.get('createdAt') > Date.now() - 7 * 24 * 60 * 60 * 1000;
} }
...@@ -1444,6 +1444,55 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -1444,6 +1444,55 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
it('should pull in dependent fields for a VIRTUAL in include', async function() {
const User = this.sequelize.define('User', {
name: Sequelize.STRING
});
const Image = this.sequelize.define('Image', {
path: {
type: Sequelize.STRING,
allowNull: false
},
url: {
type: Sequelize.VIRTUAL(Sequelize.STRING, ['path']),
get() {
return `https://my-cool-domain.com/${this.get('path')}`;
}
}
});
User.hasOne(Image);
Image.belongsTo(User);
await this.sequelize.sync({ force: true });
return User.create({
name: 'some user',
Image: {
path: 'folder1/folder2/logo.png'
}
}, {
include: {
model: Image
}
}).then(() => {
return User.findAll({
attributes: ['name'],
include: [{
model: Image,
attributes: ['url']
}]
}).then(users => {
users.forEach(user => {
expect(user.get('name')).to.equal('some user');
expect(user.Image.get('url')).to.equal('https://my-cool-domain.com/folder1/folder2/logo.png');
expect(user.Image.get('path')).to.equal('folder1/folder2/logo.png');
});
});
});
});
it('should throw for undefined where parameters', function() { it('should throw for undefined where parameters', function() {
return this.User.findAll({ where: { username: undefined } }).then(() => { return this.User.findAll({ where: { username: undefined } }).then(() => {
throw new Error('findAll should throw an error if where has a key with undefined value'); throw new Error('findAll should throw an error if where has a key with undefined value');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!