diff --git a/lib/datasource.js b/lib/datasource.js index f35a7774c..4abb46a30 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1716,7 +1716,7 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { columns.forEach(function(item) { const propName = nameMapper('column', item.columnName); - schema.properties[propName] = { + const propertyDetails = { type: item.type, required: !item.generated && (item.nullable === 'N' || item.nullable === 'NO' || item.nullable === 0 || item.nullable === false), @@ -1726,13 +1726,26 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { precision: item.dataPrecision, scale: item.dataScale, generated: item.generated || false, - }; + } + if ( + item.indexType === 'BTREE' && + item.indexName !== 'PRIMARY' && + !item.isForeignKey + ) { + propertyDetails.index = {unique: true} + } + if (pks[item.columnName]) { - schema.properties[propName].id = pks[item.columnName]; + propertyDetails[propName].id = pks[item.columnName]; } - if (uniqueKeys.includes(propName)) { - schema.properties[propName]['index'] = {unique: true}; + if ( + uniqueKeys.includes(propName) && + propertyDetails[propName]['index'] === undefined + ) { + propertyDetails[propName]['index'] = {unique: true}; } + + schema.properties[propName] = propertyDetails; const dbSpecific = schema.properties[propName][dbType] = { columnName: item.columnName, dataType: item.dataType,