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

Commit b2b0e6b6 by Mick Hansen

Merge pull request #2116 from dizlexik/master

Add attribute.field support for sync()
2 parents 3970bf71 57f1c442
......@@ -218,20 +218,24 @@ module.exports = (function() {
});
// define a new model, which connects the models
var sourceKeyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type
, targetKeyType = this.target.rawAttributes[this.target.primaryKeyAttribute].type
var sourceKey = this.source.rawAttributes[this.source.primaryKeyAttribute]
, sourceKeyType = sourceKey.type
, sourceKeyField = sourceKey.field || this.source.primaryKeyAttribute
, targetKey = this.target.rawAttributes[this.target.primaryKeyAttribute]
, targetKeyType = targetKey.type
, targetKeyField = targetKey.field || this.target.primaryKeyAttribute
, sourceAttribute = Utils._.defaults(this.foreignKeyAttribute, { type: sourceKeyType })
, targetAttribute = Utils._.defaults(this.targetAssociation.foreignKeyAttribute, { type: targetKeyType });
if (this.options.constraints !== false) {
sourceAttribute.references = this.source.getTableName();
sourceAttribute.referencesKey = this.source.primaryKeyAttribute;
sourceAttribute.referencesKey = sourceKeyField;
sourceAttribute.onDelete = this.options.onDelete || 'CASCADE';
sourceAttribute.onUpdate = this.options.onUpdate || 'CASCADE';
}
if (this.targetAssociation.options.constraints !== false) {
targetAttribute.references = this.target.getTableName();
targetAttribute.referencesKey = this.target.primaryKeyAttribute;
targetAttribute.referencesKey = targetKeyField;
targetAttribute.onDelete = this.targetAssociation.options.onDelete || 'CASCADE';
targetAttribute.onUpdate = this.targetAssociation.options.onUpdate || 'CASCADE';
}
......
......@@ -16,9 +16,9 @@ module.exports = {
if (options.foreignKeyConstraint || options.onDelete || options.onUpdate) {
// Find primary keys: composite keys not supported with this approach
var primaryKeys = Utils._.filter(Utils._.keys(source.rawAttributes), function(key) {
return source.rawAttributes[key].primaryKey;
});
var primaryKeys = Utils._.chain(source.rawAttributes).keys()
.filter(function(key) { return source.rawAttributes[key].primaryKey; })
.map(function(key) { return source.rawAttributes[key].field || key; }).value();
if (primaryKeys.length === 1) {
if (!!source.options.schema) {
......
......@@ -245,6 +245,7 @@ module.exports = (function() {
for (var name in attributes) {
var dataType = attributes[name];
var fieldName = dataType.field || name;
if (Utils._.isPlainObject(dataType)) {
var template;
......@@ -305,9 +306,9 @@ module.exports = (function() {
}
result[name] = template;
result[fieldName] = template;
} else {
result[name] = dataType;
result[fieldName] = dataType;
}
}
......
......@@ -391,6 +391,7 @@ module.exports = (function() {
for (var name in attributes) {
var dataType = attributes[name];
var fieldName = dataType.field || name;
if (Utils._.isObject(dataType)) {
var template = '<%= type %>'
......@@ -467,9 +468,9 @@ module.exports = (function() {
replacements.comment = this.escape(dataType.comment);
}
result[name] = Utils._.template(template)(replacements);
result[fieldName] = Utils._.template(template)(replacements);
} else {
result[name] = dataType;
result[fieldName] = dataType;
}
}
......
......@@ -232,6 +232,7 @@ module.exports = (function() {
for (var name in attributes) {
var dataType = attributes[name];
var fieldName = dataType.field || name;
if (Utils._.isObject(dataType)) {
var template = "<%= type %>"
......@@ -291,9 +292,9 @@ module.exports = (function() {
}
result[name] = Utils._.template(template)(replacements);
result[fieldName] = Utils._.template(template)(replacements);
} else {
result[name] = dataType;
result[fieldName] = dataType;
}
}
......
......@@ -257,27 +257,46 @@ describe(Support.getTestDialectTeaser("Model"), function () {
});
});
it('should support renaming of sequelize method fields', function () {
var User = this.sequelize.define('user', {
someProperty: Sequelize.VIRTUAL // Since we specify the AS part as a part of the literal string, not with sequelize syntax, we have to tell sequelize about the field
it('should support renaming of sequelize method fields', function () {
var Test = this.sequelize.define('test', {
someProperty: Sequelize.VIRTUAL // Since we specify the AS part as a part of the literal string, not with sequelize syntax, we have to tell sequelize about the field
});
return this.sequelize.sync({ force: true }).then(function () {
return Test.create({});
}).then(function () {
return Test.findAll({
attributes: [
Sequelize.literal('EXISTS(SELECT 1) AS "someProperty"'),
[Sequelize.literal('EXISTS(SELECT 1)'), 'someProperty2']
]
});
}).then(function (tests) {
expect(tests[0].get('someProperty')).to.be.ok;
expect(tests[0].get('someProperty2')).to.be.ok;
});
});
return this.sequelize.sync({ force: true }).then(function () {
return User.create({});
}).then(function () {
return User.findAll({
attributes: [
Sequelize.literal('EXISTS(SELECT 1) AS "someProperty"'),
[Sequelize.literal('EXISTS(SELECT 1)'), 'someProperty2']
]
it('should sync foreign keys with custom field names', function() {
return this.sequelize.sync({ force: true })
.then(function() {
var attrs = this.Task.tableAttributes;
expect(attrs.user_id.references).to.equal('users');
expect(attrs.user_id.referencesKey).to.equal('userId');
}.bind(this));
});
it('should find the value of an attribute with a custom field name', function() {
return this.User.create({ name: 'test user' })
.then(function() {
return this.User.find({ where: { name: 'test user' } });
}.bind(this))
.then(function(user) {
expect(user.name).to.equal('test user');
});
}).then(function (users) {
expect(users[0].get('someProperty')).to.be.ok;
expect(users[0].get('someProperty2')).to.be.ok;
});
});
it('field names that are the same as property names should create, update, and read correctly', function () {
it('field names that are the same as property names should create, update, and read correctly', function () {
var self = this;
return this.Comment.create({
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!