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

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() { ...@@ -394,9 +394,9 @@ module.exports = (function() {
if ((fieldsOrOptions || {}).transaction instanceof Transaction) { if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
options.transaction = fieldsOrOptions.transaction options.transaction = fieldsOrOptions.transaction
delete fieldsOrOptions.transaction
} }
if (Object(self.through) === self.through) {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
// Create the related model instance // Create the related model instance
self.target self.target
...@@ -407,6 +407,10 @@ module.exports = (function() { ...@@ -407,6 +407,10 @@ module.exports = (function() {
.proxy(emitter) .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
}), Label = this.sequelize.define('Label', {
'text': DataTypes.STRING,
'ArticleId': { 'ArticleId': {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false 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,7 +388,9 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -384,7 +388,9 @@ 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().success(function (labels) {
expect(labels.length).to.equal(0);
Label.findAll({ where: { ArticleId: article.id }}).success(function(labels) { Label.findAll({ where: { ArticleId: article.id }}).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 }}, { transaction: t }).success(function(labels) {
...@@ -400,6 +406,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -400,6 +406,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
})
describe("getting assocations with options", function() { describe("getting assocations with options", function() {
beforeEach(function(done) { beforeEach(function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!