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

Commit 55c96079 by Sebastian Wilzbach

don't overwrite options.foreignKey in associations

1 parent f482790b
# Next
- [FIXED] Don't overwrite options.foreignKey in associations [#4927](https://github.com/sequelize/sequelize/pull/4927)
# 3.14.1
- [FIXED] Issue with transaction options leaking and certain queries running outside of the transaction connection.
......
......@@ -323,8 +323,8 @@ BelongsToMany.prototype.injectAttributes = function() {
, targetKey = this.target.rawAttributes[this.target.primaryKeyAttribute]
, targetKeyType = targetKey.type
, targetKeyField = targetKey.field || this.target.primaryKeyAttribute
, sourceAttribute = _.defaults(this.foreignKeyAttribute, { type: sourceKeyType })
, targetAttribute = _.defaults(this.otherKeyAttribute, { type: targetKeyType });
, sourceAttribute = _.defaults({}, this.foreignKeyAttribute, { type: sourceKeyType })
, targetAttribute = _.defaults({}, this.otherKeyAttribute, { type: targetKeyType });
if (this.primaryKeyDeleted === true) {
targetAttribute.primaryKey = sourceAttribute.primaryKey = true;
......
......@@ -109,7 +109,7 @@ util.inherits(BelongsTo, Association);
BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {};
newAttributes[this.foreignKey] = _.defaults(this.foreignKeyAttribute, {
newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
type: this.options.keyType || this.target.rawAttributes[this.targetKey].type,
allowNull : true
});
......
......@@ -202,7 +202,7 @@ util.inherits(HasMany, Association);
HasMany.prototype.injectAttributes = function() {
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
newAttributes[this.foreignKey] = _.defaults(this.foreignKeyAttribute, {
newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
type: this.options.keyType || this.source.rawAttributes[this.source.primaryKeyAttribute].type,
allowNull : true
});
......
......@@ -103,7 +103,7 @@ HasOne.prototype.injectAttributes = function() {
var newAttributes = {}
, 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,
allowNull : true
});
......
'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 self = this;
var reqValidForeignKey = { foreignKey: { allowNull: false }};
self.A.belongsTo(self.B, reqValidForeignKey);
self.A.belongsTo(self.C, reqValidForeignKey);
expect(self.A.attributes.CId.type).to.deep.equal(self.C.attributes.id.type);
});
it('should not be overwritten for belongsToMany', function(){
var self = this;
var reqValidForeignKey = { foreignKey: { allowNull: false }, through: 'ABBridge'};
self.B.belongsToMany(self.A, reqValidForeignKey);
self.A.belongsTo(self.C, reqValidForeignKey);
expect(self.A.attributes.CId.type).to.deep.equal(self.C.attributes.id.type);
});
it('should not be overwritten for hasOne', function(){
var self = this;
var reqValidForeignKey = { foreignKey: { allowNull: false }};
self.B.hasOne(self.A, reqValidForeignKey);
self.A.belongsTo(self.C, reqValidForeignKey);
expect(self.A.attributes.CId.type).to.deep.equal(self.C.attributes.id.type);
});
it('should not be overwritten for hasMany', function(){
var self = this;
var reqValidForeignKey = { foreignKey: { allowNull: false }};
self.B.hasMany(self.A, reqValidForeignKey);
self.A.belongsTo(self.C, reqValidForeignKey);
expect(self.A.attributes.CId.type).to.deep.equal(self.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!