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

Commit 9885aca4 by Mick Hansen

1:M createAssociation should only use one query and fix transactions for createAssociation

1 parent 587ec3ad
......@@ -394,9 +394,9 @@ module.exports = (function() {
if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
options.transaction = fieldsOrOptions.transaction
delete fieldsOrOptions.transaction
}
if (Object(self.through) === self.through) {
return new Utils.CustomEventEmitter(function(emitter) {
// Create the related model instance
self.target
......@@ -407,6 +407,10 @@ module.exports = (function() {
.proxy(emitter)
})
}).run()
} else {
values[self.identifier] = instance.get(self.source.primaryKeyAttribute);
return self.target.create(values, fieldsOrOptions)
}
}
return this
......
......@@ -349,12 +349,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
it('creates the object with the association directly', function(done) {
var spy = sinon.spy()
var Article = this.sequelize.define('Article', { 'title': DataTypes.STRING })
, Label = this.sequelize.define('Label', { 'text': DataTypes.STRING,
var Article = this.sequelize.define('Article', {
'title': DataTypes.STRING
}), Label = this.sequelize.define('Label', {
'text': DataTypes.STRING,
'ArticleId': {
type: DataTypes.INTEGER,
allowNull: false
}})
}
})
Article.hasMany(Label)
......@@ -362,7 +366,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
Label.sync({ force: true }).success(function() {
Article.create({ title: 'foo' }).success(function(article) {
article.createLabel({ text: 'bar' }).on('sql', spy).complete(function(err, label) {
expect(err).to.not.be.ok
expect(err).not.to.be.ok
expect(spy.calledOnce).to.be.true
expect(label.ArticleId).to.equal(article.id)
done()
......@@ -384,7 +388,9 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
Label.sync({ force: true }).success(function() {
Article.create({ title: 'foo' }).success(function(article) {
sequelize.transaction(function (t) {
article.createLabel({ text: 'bar' }, { transaction: t }).success(function(label) {
article.createLabel({ text: 'bar' }, { transaction: t }).success(function() {
Label.findAll().success(function (labels) {
expect(labels.length).to.equal(0);
Label.findAll({ where: { ArticleId: article.id }}).success(function(labels) {
expect(labels.length).to.equal(0)
Label.findAll({ where: { ArticleId: article.id }}, { transaction: t }).success(function(labels) {
......@@ -400,6 +406,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
describe("getting assocations with options", function() {
beforeEach(function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!