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

Commit c480c35b by Sascha Depold

transaction support for belongsTo

1 parent b3bffddd
...@@ -70,14 +70,15 @@ module.exports = (function() { ...@@ -70,14 +70,15 @@ module.exports = (function() {
var self = this var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName, this.target.options.language))) , accessor = Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName, this.target.options.language)))
obj[accessor] = function(associatedObject) { obj[accessor] = function(associatedObject, options) {
var primaryKeys = !!associatedObject && !!associatedObject.daoFactory ? Object.keys(associatedObject.daoFactory.primaryKeys) : [] var primaryKeys = !!associatedObject && !!associatedObject.daoFactory ? Object.keys(associatedObject.daoFactory.primaryKeys) : []
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null
options = Utils._.extend({ fields: [ self.identifier ], allowNull: [self.identifier] }, options)
// passes the changed field to save, so only that field get updated. // passes the changed field to save, so only that field get updated.
return this.save([ self.identifier ], {allowNull: [self.identifier]}) return this.save(options)
} }
return this return this
......
...@@ -9,7 +9,7 @@ chai.Assertion.includeStack = true ...@@ -9,7 +9,7 @@ chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("BelongsTo"), function() { describe(Support.getTestDialectTeaser("BelongsTo"), function() {
describe("Model.associations", function () { describe("Model.associations", function () {
it("should store all assocations when associting to the same table multiple times", function () { it("should store all assocations when associting to the same table multiple times", function () {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {}) , Group = this.sequelize.define('Group', {})
Group.belongsTo(User) Group.belongsTo(User)
...@@ -21,6 +21,32 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -21,6 +21,32 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
describe('setAssociation', function() { describe('setAssociation', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.belongsTo(User)
sequelize.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction(function(t) {
group.setUser(user, { transaction: t }).success(function() {
Group.all().success(function(groups) {
groups[0].getUser().success(function(associatedUser) {
expect(associatedUser).to.be.null
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
})
it('can set the association with declared primary keys...', function(done) { it('can set the association with declared primary keys...', function(done) {
var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING }) var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING })
, Task = this.sequelize.define('TaskXYZ', { task_id: {type: DataTypes.INTEGER, primaryKey: true }, title: DataTypes.STRING }) , Task = this.sequelize.define('TaskXYZ', { task_id: {type: DataTypes.INTEGER, primaryKey: true }, title: DataTypes.STRING })
...@@ -207,15 +233,15 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -207,15 +233,15 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
describe("Association column", function() { describe("Association column", function() {
it('has correct type for non-id primary keys with non-integer type', function(done) { it('has correct type for non-id primary keys with non-integer type', function(done) {
var User = this.sequelize.define('UserPKBT', { var User = this.sequelize.define('UserPKBT', {
username: { username: {
type: DataTypes.STRING type: DataTypes.STRING
} }
}) })
, self = this , self = this
var Group = this.sequelize.define('GroupPKBT', { var Group = this.sequelize.define('GroupPKBT', {
name: { name: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true primaryKey: true
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!