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

Commit 917b5bdb by javiertury Committed by Sushant

fix(associations): symmetric getters/setters on foreign keys (#10635)

1 parent dc6d816d
...@@ -3868,7 +3868,7 @@ class Model { ...@@ -3868,7 +3868,7 @@ class Model {
return include.association.throughModel.create(values, includeOptions); return include.association.throughModel.create(values, includeOptions);
}); });
} }
instance.set(include.association.foreignKey, this.get(include.association.sourceKey || this.constructor.primaryKeyAttribute, { raw: true })); instance.set(include.association.foreignKey, this.get(include.association.sourceKey || this.constructor.primaryKeyAttribute, { raw: true }), { raw: true });
Object.assign(instance, include.association.scope); Object.assign(instance, include.association.scope);
return instance.save(includeOptions); return instance.save(includeOptions);
}); });
......
...@@ -1590,6 +1590,73 @@ describe(Support.getTestDialectTeaser('HasMany'), () => { ...@@ -1590,6 +1590,73 @@ describe(Support.getTestDialectTeaser('HasMany'), () => {
expect(tasks[0].title).to.be.equal(values.tasks[0].title, 'task title is correct'); expect(tasks[0].title).to.be.equal(values.tasks[0].title, 'task title is correct');
})); }));
}); });
it('should create nested associations with symmetric getters/setters on FK', function() {
// Dummy getter/setter to test they are symmetric
function toCustomFormat(string) {
return string && `FORMAT-${string}`;
}
function fromCustomFormat(string) {
return string && string.slice(7);
}
const Parent = this.sequelize.define('Parent', {
id: {
type: DataTypes.STRING,
primaryKey: true,
get() {
return fromCustomFormat(this.getDataValue('id'));
},
set(value) {
this.setDataValue('id', toCustomFormat(value));
}
}
});
const Child = this.sequelize.define('Child', {
id: {
type: DataTypes.STRING,
primaryKey: true
},
parent: {
type: DataTypes.STRING,
get() {
return fromCustomFormat(this.getDataValue('parent'));
},
set(value) {
this.setDataValue('parent', toCustomFormat(value));
}
}
});
Child.belongsTo(Parent, {
foreignKey: 'parent',
targetKey: 'id'
});
Parent.hasMany(Child, {
foreignKey: 'parent',
targetKey: 'id',
as: 'children'
});
const values = {
id: 'sJn369d8Em',
children: [{ id: 'dgeQAQaW7A' }]
};
return this.sequelize.sync({ force: true })
.then(() => Parent.create(values, { include: { model: Child, as: 'children' } }))
.then(father => {
// Make sure tasks are defined for created user
expect(father.id).to.be.equal('sJn369d8Em');
expect(father.get('id', { raw: true })).to.be.equal('FORMAT-sJn369d8Em');
expect(father).to.have.property('children');
expect(father.children).to.be.an('array');
expect(father.children).to.lengthOf(1);
expect(father.children[0].parent).to.be.equal('sJn369d8Em');
expect(father.children[0].get('parent', { raw: true })).to.be.equal('FORMAT-sJn369d8Em');
});
});
}); });
describe('sourceKey with where clause in include', () => { describe('sourceKey with where clause in include', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!