Create model foreign key matching type of opposite part of relation (even if it has a custom field type)

This commit is contained in:
Andrey Loukhnov 2015-02-03 13:13:00 +03:00
parent ac547433b1
commit e68ecb461a
1 changed files with 44 additions and 39 deletions

View File

@ -1230,8 +1230,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
options: {
idInjection: false // DO NOT add id property
},
properties: {
}
properties: {}
};
schema.options[schemaName] = {
@ -1373,8 +1372,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
options: {
idInjection: false // DO NOT add id property
},
properties: {
}
properties: {}
};
schema.options[schemaName] = {
@ -1711,11 +1709,18 @@ DataSource.prototype.defineForeignKey = function defineForeignKey(className, key
return;
}
var fkDef = {type: pkType};
var foreignMeta = this.columnMetadata(foreignClassName, key);
if(foreignMeta && foreignMeta.dataType) {
fkDef[this.connector.name] = {};
fkDef[this.connector.name].dataType = foreignMeta.dataType;
}
if (this.connector.defineForeignKey) {
var cb = function (err, keyType) {
if (err) throw err;
fkDef.type = keyType || pkType;
// Add the foreign key property to the data source _models
this.defineProperty(className, key, {type: keyType || pkType});
this.defineProperty(className, key, fkDef);
}.bind(this);
switch (this.connector.defineForeignKey.length) {
case 4:
@ -1728,7 +1733,7 @@ DataSource.prototype.defineForeignKey = function defineForeignKey(className, key
}
} else {
// Add the foreign key property to the data source _models
this.defineProperty(className, key, {type: pkType});
this.defineProperty(className, key, fkDef);
}
};