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

Commit 9885aca4 by Mick Hansen

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

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