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

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() { ...@@ -218,20 +218,24 @@ module.exports = (function() {
}); });
// define a new model, which connects the models // define a new model, which connects the models
var sourceKeyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type var sourceKey = this.source.rawAttributes[this.source.primaryKeyAttribute]
, targetKeyType = this.target.rawAttributes[this.target.primaryKeyAttribute].type , 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 }) , sourceAttribute = Utils._.defaults(this.foreignKeyAttribute, { type: sourceKeyType })
, targetAttribute = Utils._.defaults(this.targetAssociation.foreignKeyAttribute, { type: targetKeyType }); , targetAttribute = Utils._.defaults(this.targetAssociation.foreignKeyAttribute, { type: targetKeyType });
if (this.options.constraints !== false) { if (this.options.constraints !== false) {
sourceAttribute.references = this.source.getTableName(); sourceAttribute.references = this.source.getTableName();
sourceAttribute.referencesKey = this.source.primaryKeyAttribute; sourceAttribute.referencesKey = sourceKeyField;
sourceAttribute.onDelete = this.options.onDelete || 'CASCADE'; sourceAttribute.onDelete = this.options.onDelete || 'CASCADE';
sourceAttribute.onUpdate = this.options.onUpdate || 'CASCADE'; sourceAttribute.onUpdate = this.options.onUpdate || 'CASCADE';
} }
if (this.targetAssociation.options.constraints !== false) { if (this.targetAssociation.options.constraints !== false) {
targetAttribute.references = this.target.getTableName(); targetAttribute.references = this.target.getTableName();
targetAttribute.referencesKey = this.target.primaryKeyAttribute; targetAttribute.referencesKey = targetKeyField;
targetAttribute.onDelete = this.targetAssociation.options.onDelete || 'CASCADE'; targetAttribute.onDelete = this.targetAssociation.options.onDelete || 'CASCADE';
targetAttribute.onUpdate = this.targetAssociation.options.onUpdate || 'CASCADE'; targetAttribute.onUpdate = this.targetAssociation.options.onUpdate || 'CASCADE';
} }
......
...@@ -16,9 +16,9 @@ module.exports = { ...@@ -16,9 +16,9 @@ module.exports = {
if (options.foreignKeyConstraint || options.onDelete || options.onUpdate) { if (options.foreignKeyConstraint || options.onDelete || options.onUpdate) {
// Find primary keys: composite keys not supported with this approach // Find primary keys: composite keys not supported with this approach
var primaryKeys = Utils._.filter(Utils._.keys(source.rawAttributes), function(key) { var primaryKeys = Utils._.chain(source.rawAttributes).keys()
return source.rawAttributes[key].primaryKey; .filter(function(key) { return source.rawAttributes[key].primaryKey; })
}); .map(function(key) { return source.rawAttributes[key].field || key; }).value();
if (primaryKeys.length === 1) { if (primaryKeys.length === 1) {
if (!!source.options.schema) { if (!!source.options.schema) {
......
...@@ -245,6 +245,7 @@ module.exports = (function() { ...@@ -245,6 +245,7 @@ module.exports = (function() {
for (var name in attributes) { for (var name in attributes) {
var dataType = attributes[name]; var dataType = attributes[name];
var fieldName = dataType.field || name;
if (Utils._.isPlainObject(dataType)) { if (Utils._.isPlainObject(dataType)) {
var template; var template;
...@@ -305,9 +306,9 @@ module.exports = (function() { ...@@ -305,9 +306,9 @@ module.exports = (function() {
} }
result[name] = template; result[fieldName] = template;
} else { } else {
result[name] = dataType; result[fieldName] = dataType;
} }
} }
......
...@@ -391,6 +391,7 @@ module.exports = (function() { ...@@ -391,6 +391,7 @@ module.exports = (function() {
for (var name in attributes) { for (var name in attributes) {
var dataType = attributes[name]; var dataType = attributes[name];
var fieldName = dataType.field || name;
if (Utils._.isObject(dataType)) { if (Utils._.isObject(dataType)) {
var template = '<%= type %>' var template = '<%= type %>'
...@@ -467,9 +468,9 @@ module.exports = (function() { ...@@ -467,9 +468,9 @@ module.exports = (function() {
replacements.comment = this.escape(dataType.comment); replacements.comment = this.escape(dataType.comment);
} }
result[name] = Utils._.template(template)(replacements); result[fieldName] = Utils._.template(template)(replacements);
} else { } else {
result[name] = dataType; result[fieldName] = dataType;
} }
} }
......
...@@ -232,6 +232,7 @@ module.exports = (function() { ...@@ -232,6 +232,7 @@ module.exports = (function() {
for (var name in attributes) { for (var name in attributes) {
var dataType = attributes[name]; var dataType = attributes[name];
var fieldName = dataType.field || name;
if (Utils._.isObject(dataType)) { if (Utils._.isObject(dataType)) {
var template = "<%= type %>" var template = "<%= type %>"
...@@ -291,9 +292,9 @@ module.exports = (function() { ...@@ -291,9 +292,9 @@ module.exports = (function() {
} }
result[name] = Utils._.template(template)(replacements); result[fieldName] = Utils._.template(template)(replacements);
} else { } else {
result[name] = dataType; result[fieldName] = dataType;
} }
} }
......
...@@ -257,27 +257,46 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -257,27 +257,46 @@ describe(Support.getTestDialectTeaser("Model"), function () {
}); });
}); });
it('should support renaming of sequelize method fields', function () { it('should support renaming of sequelize method fields', function () {
var User = this.sequelize.define('user', { 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 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 () { it('should sync foreign keys with custom field names', function() {
return User.create({}); return this.sequelize.sync({ force: true })
}).then(function () { .then(function() {
return User.findAll({ var attrs = this.Task.tableAttributes;
attributes: [ expect(attrs.user_id.references).to.equal('users');
Sequelize.literal('EXISTS(SELECT 1) AS "someProperty"'), expect(attrs.user_id.referencesKey).to.equal('userId');
[Sequelize.literal('EXISTS(SELECT 1)'), 'someProperty2'] }.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; var self = this;
return this.Comment.create({ 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!