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

Commit e92577ee by Mick Hansen

Merge branch 'master' of github.com:sequelize/sequelize

Conflicts:
	changelog.md
2 parents 4244ac8d 1b54275a
# Next] # Next
- [FIXED] Model.aggregate methods now support attributes and where conditions with fields. [#4935](https://github.com/sequelize/sequelize/issues/4935) - [FIXED] Model.aggregate methods now support attributes and where conditions with fields. [#4935](https://github.com/sequelize/sequelize/issues/4935)
- [FIXED] Don't overwrite options.foreignKey in associations [#4927](https://github.com/sequelize/sequelize/pull/4927)
# 3.14.1 # 3.14.1
- [FIXED] Issue with transaction options leaking and certain queries running outside of the transaction connection. - [FIXED] Issue with transaction options leaking and certain queries running outside of the transaction connection.
......
...@@ -53,7 +53,8 @@ sequelize ...@@ -53,7 +53,8 @@ sequelize
.authenticate() .authenticate()
.then(function(err) { .then(function(err) {
console.log('Connection has been established successfully.'); console.log('Connection has been established successfully.');
}, function (err) { })
.catch(function (err) {
console.log('Unable to connect to the database:', err); console.log('Unable to connect to the database:', err);
}); });
``` ```
......
...@@ -510,12 +510,12 @@ Project.create({ /* */ }).then(function(project) { ...@@ -510,12 +510,12 @@ Project.create({ /* */ }).then(function(project) {
// check if all associated objects are as expected: // check if all associated objects are as expected:
// let's assume we have already a project and two users // let's assume we have already a project and two users
project.setUsers([user1, user2]).then(function() { project.setUsers([user1, user2]).then(function() {
return project.hasUsers([user1]).then(function(result) { return project.hasUsers([user1]);
// result would be false }).then(function(result) {
return project.hasUsers([user1, user2]).then(function(result) { // result would be false
// result would be true return project.hasUsers([user1, user2]);
}) }).then(function(result) {
}) // result would be true
}) })
``` ```
......
...@@ -113,10 +113,9 @@ Once you created an object and got a reference to it, you can delete it fr ...@@ -113,10 +113,9 @@ Once you created an object and got a reference to it, you can delete it fr
```js ```js
Task.create({ title: 'a task' }).then(function(task) { Task.create({ title: 'a task' }).then(function(task) {
// now you see me... // now you see me...
  return task.destroy();
task.destroy().then(function() { }).then(function() {
// now i'm gone :)  // now i'm gone :)
})
}) })
``` ```
...@@ -144,9 +143,9 @@ User.bulkCreate([ ...@@ -144,9 +143,9 @@ User.bulkCreate([
{ username: 'foo', isAdmin: true }, { username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false } { username: 'bar', isAdmin: false }
]).then(function() { // Notice: There are no arguments here, as of right now you'll have to... ]).then(function() { // Notice: There are no arguments here, as of right now you'll have to...
User.findAll().then(function(users) { return User.findAll();
console.log(users) // ... in order to get the array of user objects }).then(function(users) {
}) console.log(users) // ... in order to get the array of user objects
}) })
``` ```
...@@ -158,15 +157,15 @@ Task.bulkCreate([ ...@@ -158,15 +157,15 @@ Task.bulkCreate([
{subject: 'reading', status: 'executing'}, {subject: 'reading', status: 'executing'},
{subject: 'programming', status: 'finished'} {subject: 'programming', status: 'finished'}
]).then(function() { ]).then(function() {
Task.update( return Task.update(
{ status: 'inactive' } /* set attributes' value */, { status: 'inactive' }, /* set attributes' value */,
{ where: { subject: 'programming' }} /* where criteria */ { where: { subject: 'programming' }} /* where criteria */
).then(function(affectedRows) { );
// affectedRows will be 2 }).then(function(affectedRows) {
Task.findAll().then(function(tasks) { // affectedRows will be 2
console.log(tasks) // the 'programming' tasks will both have a status of 'inactive' return Task.findAll();
}) }).then(function(tasks) {
}) console.log(tasks) // the 'programming' tasks will both have a status of 'inactive'
}) })
``` ```
...@@ -178,17 +177,17 @@ Task.bulkCreate([ ...@@ -178,17 +177,17 @@ Task.bulkCreate([
{subject: 'reading', status: 'executing'}, {subject: 'reading', status: 'executing'},
{subject: 'programming', status: 'finished'} {subject: 'programming', status: 'finished'}
]).then(function() { ]).then(function() {
Task.destroy({ return Task.destroy({
where: { where: {
subject: 'programming' subject: 'programming'
}, },
truncate: true /* this will ignore where and truncate the table instead */ truncate: true /* this will ignore where and truncate the table instead */
}).then(function(affectedRows) { });
// affectedRows will be 2 }).then(function(affectedRows) {
Task.findAll().then(function(tasks) { // affectedRows will be 2
console.log(tasks) // no programming, just reading :( return Task.findAll();
}) }).then(function(tasks) {
}) console.log(tasks) // no programming, just reading :(
}) })
``` ```
...@@ -294,27 +293,27 @@ First of all you can define a field and the value you want to add to it. ...@@ -294,27 +293,27 @@ First of all you can define a field and the value you want to add to it.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.increment('my-integer-field', {by: 2}).then(/* ... */) return user.increment('my-integer-field', {by: 2})
}) }).then(/* ... */)
``` ```
Second, you can define multiple fields and the value you want to add to them. Second, you can define multiple fields and the value you want to add to them.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2}).then(/* ... */) return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}) }).then(/* ... */)
``` ```
Third, you can define an object containing fields and its increment values. Third, you can define an object containing fields and its increment values.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.increment({ return user.increment({
'my-integer-field': 2, 'my-integer-field': 2,
'my-very-other-field': 3 'my-very-other-field': 3
}).then(/* ... */) })
}) }).then(/* ... */)
``` ```
## Decrementing certain values of an instance ## Decrementing certain values of an instance
...@@ -325,25 +324,25 @@ First of all you can define a field and the value you want to add to it. ...@@ -325,25 +324,25 @@ First of all you can define a field and the value you want to add to it.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.decrement('my-integer-field', {by: 2}).then(/* ... */) return user.decrement('my-integer-field', {by: 2})
}) }).then(/* ... */)
``` ```
Second, you can define multiple fields and the value you want to add to them. Second, you can define multiple fields and the value you want to add to them.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: 2}).then(/* ... */) return user.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}) }).then(/* ... */)
``` ```
Third, you can define an object containing fields and its decrement values. Third, you can define an object containing fields and its decrement values.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.decrement({ return user.decrement({
'my-integer-field': 2, 'my-integer-field': 2,
'my-very-other-field': 3 'my-very-other-field': 3
}).then(/* ... */) })
}) }).then(/* ... */)
``` ```
...@@ -146,7 +146,7 @@ $col: 'user.organization_id' // = "user"."organization_id", with dialect specifi ...@@ -146,7 +146,7 @@ $col: 'user.organization_id' // = "user"."organization_id", with dialect specifi
{ {
rank: { rank: {
$or: { $or: {
$lt: 100, $lt: 1000,
$eq: null $eq: null
} }
} }
......
...@@ -24,10 +24,10 @@ sequelize.sync().then(function() { ...@@ -24,10 +24,10 @@ sequelize.sync().then(function() {
return User.create({ return User.create({
username: 'janedoe', username: 'janedoe',
birthday: new Date(1980, 6, 20) birthday: new Date(1980, 6, 20)
}).then(function(jane) { });
console.log(jane.get({ }).then(function(jane) {
plain: true console.log(jane.get({
})) plain: true
}); }));
}); });
``` ```
...@@ -323,8 +323,8 @@ BelongsToMany.prototype.injectAttributes = function() { ...@@ -323,8 +323,8 @@ BelongsToMany.prototype.injectAttributes = function() {
, targetKey = this.target.rawAttributes[this.target.primaryKeyAttribute] , targetKey = this.target.rawAttributes[this.target.primaryKeyAttribute]
, targetKeyType = targetKey.type , targetKeyType = targetKey.type
, targetKeyField = targetKey.field || this.target.primaryKeyAttribute , targetKeyField = targetKey.field || this.target.primaryKeyAttribute
, sourceAttribute = _.defaults(this.foreignKeyAttribute, { type: sourceKeyType }) , sourceAttribute = _.defaults({}, this.foreignKeyAttribute, { type: sourceKeyType })
, targetAttribute = _.defaults(this.otherKeyAttribute, { type: targetKeyType }); , targetAttribute = _.defaults({}, this.otherKeyAttribute, { type: targetKeyType });
if (this.primaryKeyDeleted === true) { if (this.primaryKeyDeleted === true) {
targetAttribute.primaryKey = sourceAttribute.primaryKey = true; targetAttribute.primaryKey = sourceAttribute.primaryKey = true;
......
...@@ -109,7 +109,7 @@ util.inherits(BelongsTo, Association); ...@@ -109,7 +109,7 @@ util.inherits(BelongsTo, Association);
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {}; var newAttributes = {};
newAttributes[this.foreignKey] = _.defaults(this.foreignKeyAttribute, { newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
type: this.options.keyType || this.target.rawAttributes[this.targetKey].type, type: this.options.keyType || this.target.rawAttributes[this.targetKey].type,
allowNull : true allowNull : true
}); });
......
...@@ -202,7 +202,7 @@ util.inherits(HasMany, Association); ...@@ -202,7 +202,7 @@ util.inherits(HasMany, Association);
HasMany.prototype.injectAttributes = function() { HasMany.prototype.injectAttributes = function() {
var newAttributes = {}; var newAttributes = {};
var constraintOptions = _.clone(this.options); // Create a new options object for use with addForeignKeyConstraints, to avoid polluting this.options in case it is later used for a n:m var constraintOptions = _.clone(this.options); // Create a new options object for use with addForeignKeyConstraints, to avoid polluting this.options in case it is later used for a n:m
newAttributes[this.foreignKey] = _.defaults(this.foreignKeyAttribute, { newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
type: this.options.keyType || this.source.rawAttributes[this.source.primaryKeyAttribute].type, type: this.options.keyType || this.source.rawAttributes[this.source.primaryKeyAttribute].type,
allowNull : true allowNull : true
}); });
......
...@@ -103,7 +103,7 @@ HasOne.prototype.injectAttributes = function() { ...@@ -103,7 +103,7 @@ HasOne.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
, keyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type; , keyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type;
newAttributes[this.foreignKey] = _.defaults(this.foreignKeyAttribute, { newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
type: this.options.keyType || keyType, type: this.options.keyType || keyType,
allowNull : true allowNull : true
}); });
......
...@@ -97,6 +97,7 @@ ConnectionManager.prototype.initPools = function () { ...@@ -97,6 +97,7 @@ ConnectionManager.prototype.initPools = function () {
}, },
destroy: function(connection) { destroy: function(connection) {
self.$disconnect(connection); self.$disconnect(connection);
return null;
}, },
max: config.pool.max, max: config.pool.max,
min: config.pool.min, min: config.pool.min,
...@@ -174,6 +175,7 @@ ConnectionManager.prototype.initPools = function () { ...@@ -174,6 +175,7 @@ ConnectionManager.prototype.initPools = function () {
}, },
destroy: function(connection) { destroy: function(connection) {
self.$disconnect(connection); self.$disconnect(connection);
return null;
}, },
validate: config.pool.validate, validate: config.pool.validate,
max: config.pool.max, max: config.pool.max,
...@@ -191,6 +193,7 @@ ConnectionManager.prototype.initPools = function () { ...@@ -191,6 +193,7 @@ ConnectionManager.prototype.initPools = function () {
}, },
destroy: function(connection) { destroy: function(connection) {
self.$disconnect(connection); self.$disconnect(connection);
return null;
}, },
validate: config.pool.validate, validate: config.pool.validate,
max: config.pool.max, max: config.pool.max,
...@@ -221,6 +224,7 @@ ConnectionManager.prototype.getConnection = function(options) { ...@@ -221,6 +224,7 @@ ConnectionManager.prototype.getConnection = function(options) {
self.versionPromise = null; self.versionPromise = null;
self.$disconnect(connection); self.$disconnect(connection);
return null;
}); });
}).catch(function (err) { }).catch(function (err) {
self.versionPromise = null; self.versionPromise = null;
......
...@@ -2450,6 +2450,7 @@ Model.prototype.update = function(values, options) { ...@@ -2450,6 +2450,7 @@ Model.prototype.update = function(values, options) {
delete options.attributes; delete options.attributes;
}); });
} }
return null;
}).then(function() { }).then(function() {
valuesUse = values; valuesUse = values;
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, Sequelize = require('../../../index');
describe(Support.getTestDialectTeaser('associations'), function() {
describe('Test options.foreignKey', function() {
beforeEach(function() {
this.A = this.sequelize.define('A', {
id: {
type: DataTypes.CHAR(20),
primaryKey: true
}
});
this.B = this.sequelize.define('B', {
id: {
type: Sequelize.CHAR(20),
primaryKey: true
}
});
this.C = this.sequelize.define('C', {});
});
it('should not be overwritten for belongsTo', function(){
var reqValidForeignKey = { foreignKey: { allowNull: false }};
this.A.belongsTo(this.B, reqValidForeignKey);
this.A.belongsTo(this.C, reqValidForeignKey);
expect(this.A.attributes.CId.type).to.deep.equal(this.C.attributes.id.type);
});
it('should not be overwritten for belongsToMany', function(){
var reqValidForeignKey = { foreignKey: { allowNull: false }, through: 'ABBridge'};
this.B.belongsToMany(this.A, reqValidForeignKey);
this.A.belongsTo(this.C, reqValidForeignKey);
expect(this.A.attributes.CId.type).to.deep.equal(this.C.attributes.id.type);
});
it('should not be overwritten for hasOne', function(){
var reqValidForeignKey = { foreignKey: { allowNull: false }};
this.B.hasOne(this.A, reqValidForeignKey);
this.A.belongsTo(this.C, reqValidForeignKey);
expect(this.A.attributes.CId.type).to.deep.equal(this.C.attributes.id.type);
});
it('should not be overwritten for hasMany', function(){
var reqValidForeignKey = { foreignKey: { allowNull: false }};
this.B.hasMany(this.A, reqValidForeignKey);
this.A.belongsTo(this.C, reqValidForeignKey);
expect(this.A.attributes.CId.type).to.deep.equal(this.C.attributes.id.type);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!