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

Commit e7c0351e by Joe Lutz

Add attribute.field support for foreign keys

1 parent dd4ceb85
......@@ -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) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!