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

Commit 23453399 by Jan Aagaard Meier

Re-calcualte primary keys when calling refreshAttributes. Fixed #3652

1 parent 7888734e
......@@ -174,7 +174,8 @@ var addDefaultAttributes = function() {
, head = {};
// Add id if no primary key was manually added to definition
if (!Object.keys(this.primaryKeys).length) {
// Can't use this.primaryKeys here, since this function is called before PKs are identified
if (!_.any(this.rawAttributes, 'primaryKey')) {
head = {
id: {
type: new DataTypes.INTEGER(),
......@@ -580,7 +581,6 @@ Model.prototype.init = function(modelManager) {
this.modelManager = modelManager;
this.primaryKeys = {};
self.options.uniqueKeys = {};
// Setup names of timestamp attributes
this._timestampAttributes = {};
......@@ -596,51 +596,8 @@ Model.prototype.init = function(modelManager) {
}
}
// Identify primary and unique attributes
Utils._.each(this.rawAttributes, function(options, attribute) {
if (options.hasOwnProperty('unique')) {
var idxName;
if (options.unique === true) {
idxName = self.tableName + '_' + attribute + '_unique';
self.options.uniqueKeys[idxName] = {
name: idxName,
fields: [attribute],
singleField: true
};
} else if (options.unique !== false) {
idxName = options.unique;
if (typeof options.unique === 'object') {
idxName = options.unique.name;
}
self.options.uniqueKeys[idxName] = self.options.uniqueKeys[idxName] || {fields: [], msg: null};
self.options.uniqueKeys[idxName].fields.push(options.field || attribute);
self.options.uniqueKeys[idxName].msg = self.options.uniqueKeys[idxName].msg || options.unique.msg || null;
self.options.uniqueKeys[idxName].name = idxName || false;
}
}
if (options.primaryKey === true) {
self.primaryKeys[attribute] = self.attributes[attribute];
}
});
this.uniqueKeys = this.options.uniqueKeys;
// Add head and tail default attributes (id, timestamps)
addDefaultAttributes.call(this);
addOptionalClassMethods.call(this);
// Primary key convenience variables
this.primaryKeyAttributes = Object.keys(this.primaryKeys);
this.primaryKeyAttribute = this.primaryKeyAttributes[0];
this.primaryKeyField = this.rawAttributes[this.primaryKeyAttribute].field || this.primaryKeyAttribute;
this.primaryKeyCount = this.primaryKeyAttributes.length;
this._hasPrimaryKeys = this.options.hasPrimaryKeys = this.hasPrimaryKeys = this.primaryKeyCount > 0;
this._isPrimaryKey = Utils._.memoize(function(key) {
return self.primaryKeyAttributes.indexOf(key) !== -1;
});
this.$scope = _.isPlainObject(this.options.defaultScope) ? this.options.defaultScope : {};
......@@ -668,7 +625,10 @@ Model.prototype.init = function(modelManager) {
self.Instance.prototype[name] = fct;
});
}
addDefaultAttributes.call(this);
this.refreshAttributes();
findAutoIncrementField.call(this);
this.Instance.prototype.$Model =
......@@ -743,6 +703,9 @@ Model.prototype.refreshAttributes = function() {
this.fieldRawAttributesMap = {};
this.primaryKeys = {};
self.options.uniqueKeys = {};
Utils._.each(this.rawAttributes, function(definition, name) {
definition.type = self.sequelize.normalizeDataType(definition.type);
......@@ -754,6 +717,10 @@ Model.prototype.refreshAttributes = function() {
definition.field = name;
}
if (definition.primaryKey === true) {
self.primaryKeys[name] = definition;
}
self.fieldRawAttributesMap[definition.field] = definition;
if (definition.type instanceof DataTypes.BOOLEAN) {
......@@ -784,6 +751,28 @@ Model.prototype.refreshAttributes = function() {
self._defaultValues[name] = Utils._.partial(Utils.toDefaultValue, definition.defaultValue);
}
if (definition.hasOwnProperty('unique')) {
var idxName;
if (definition.unique === true) {
idxName = self.tableName + '_' + name + '_unique';
self.options.uniqueKeys[idxName] = {
name: idxName,
fields: [definition.field],
singleField: true
};
} else if (definition.unique !== false) {
idxName = definition.unique;
if (typeof definition.unique === 'object') {
idxName = definition.unique.name;
}
self.options.uniqueKeys[idxName] = self.options.uniqueKeys[idxName] || {fields: [], msg: null};
self.options.uniqueKeys[idxName].fields.push(definition.field);
self.options.uniqueKeys[idxName].msg = self.options.uniqueKeys[idxName].msg || definition.unique.msg || null;
self.options.uniqueKeys[idxName].name = idxName || false;
}
}
if (definition.hasOwnProperty('validate')) {
self.Instance.prototype.validators[name] = definition.validate;
}
......@@ -798,6 +787,8 @@ Model.prototype.refreshAttributes = function() {
}
});
this.uniqueKeys = this.options.uniqueKeys;
this._hasBooleanAttributes = !!this._booleanAttributes.length;
this._isBooleanAttribute = Utils._.memoize(function(key) {
return self._booleanAttributes.indexOf(key) !== -1;
......@@ -848,6 +839,20 @@ Model.prototype.refreshAttributes = function() {
this.Instance.prototype._isAttribute = Utils._.memoize(function(key) {
return self.Instance.prototype.attributes.indexOf(key) !== -1;
});
// Primary key convenience variables
this.primaryKeyAttributes = Object.keys(this.primaryKeys);
this.primaryKeyAttribute = this.primaryKeyAttributes[0];
if (this.primaryKeyAttribute) {
this.primaryKeyField = this.rawAttributes[this.primaryKeyAttribute].field || this.primaryKeyAttribute;
}
this.primaryKeyCount = this.primaryKeyAttributes.length;
this._hasPrimaryKeys = this.options.hasPrimaryKeys = this.hasPrimaryKeys = this.primaryKeyCount > 0;
this._isPrimaryKey = Utils._.memoize(function(key) {
return self.primaryKeyAttributes.indexOf(key) !== -1;
});
};
/**
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, _ = require('lodash')
, DataTypes = require(__dirname + '/../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('removeAttribute', function () {
it('should support removing the primary key', function () {
var Model = current.define('m', {
name: DataTypes.STRING
});
expect(Model.primaryKeyAttribute).not.to.be.undefined;
expect(_.size(Model.primaryKeys)).to.equal(1);
Model.removeAttribute('id');
expect(Model.primaryKeyAttribute).to.be.undefined;
expect(_.size(Model.primaryKeys)).to.equal(0);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!