diff --git a/src/generators/mssql.ts b/src/generators/mssql.ts index df56f89..a451529 100644 --- a/src/generators/mssql.ts +++ b/src/generators/mssql.ts @@ -223,8 +223,9 @@ export default class MSSQLGenerator { output += EOL; } - foreignKeys.forEach((fk) => { - output += this.foreignKey(fk); + const groupedForeignKeys = Helpers.groupByName(foreignKeys, 'name'); + Object.keys(groupedForeignKeys).forEach((name) => { + output += this.foreignKey(groupedForeignKeys[name]); output += EOL; }); @@ -514,13 +515,11 @@ export default class MSSQLGenerator { case 'char': case 'varbinary': case 'binary': - case 'text': size = item.max_length === -1 ? 'max' : item.max_length; output += `(${size})`; break; case 'nvarchar': case 'nchar': - case 'ntext': size = item.max_length === -1 ? 'max' : item.max_length / 2; output += `(${size})`; break; @@ -616,11 +615,14 @@ export default class MSSQLGenerator { * * @param item Row from foreignKeys query. */ - private foreignKey(item: SqlForeignKey): string { - const objectId = `[${item.schema}].[${item.table}]`; - const keyObjectId = `[${item.schema}].[${item.name}]`; - const parentObjectId = `[${item.parent_schema}].[${item.parent_table}]`; + private foreignKey(items: SqlForeignKey[]): string { + const first = items[0]; + const objectId = `[${first.schema}].[${first.table}]`; + const keyObjectId = `[${first.schema}].[${first.name}]`; + const parentObjectId = `[${first.parent_schema}].[${first.parent_table}]`; let output = ''; + const columns: string[] = []; + const references: string[] = []; output += `IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE object_id = OBJECT_ID('${keyObjectId}') AND parent_object_id = OBJECT_ID('${objectId}'))`; output += EOL; @@ -630,12 +632,18 @@ export default class MSSQLGenerator { output += this.indent() + `ALTER TABLE ${objectId} WITH ${ - item.is_not_trusted ? 'NOCHECK' : 'CHECK' + first.is_not_trusted ? 'NOCHECK' : 'CHECK' }`; - output += ` ADD CONSTRAINT [${item.name}] FOREIGN KEY ([${item.column}])`; - output += ` REFERENCES ${parentObjectId} ([${item.reference}])`; + items.forEach((item) => { + columns.push(item.column); + references.push(item.reference); + }); + output += ` ADD CONSTRAINT [${first.name}] FOREIGN KEY ([${columns.join( + ', ' + )}])`; + output += ` REFERENCES ${parentObjectId} ([${references.join(', ')}])`; - switch (item.delete_referential_action) { + switch (first.delete_referential_action) { case 1: output += ' ON DELETE CASCADE'; break; @@ -647,7 +655,7 @@ export default class MSSQLGenerator { break; } - switch (item.update_referential_action) { + switch (first.update_referential_action) { case 1: output += ' ON UPDATE CASCADE'; break; @@ -661,7 +669,8 @@ export default class MSSQLGenerator { output += EOL; output += - this.indent() + `ALTER TABLE ${objectId} CHECK CONSTRAINT [${item.name}]`; + this.indent() + + `ALTER TABLE ${objectId} CHECK CONSTRAINT [${first.name}]`; output += EOL; output += 'END'; output += EOL; diff --git a/src/queries/mssql.ts b/src/queries/mssql.ts index 6d28f8b..7eed877 100644 --- a/src/queries/mssql.ts +++ b/src/queries/mssql.ts @@ -108,7 +108,8 @@ export const foreignKeysRead = ` SCHEMA_NAME(ro.schema_id) AS [parent_schema], ro.name AS [parent_table], fk.delete_referential_action, - fk.update_referential_action + fk.update_referential_action, + fkc.constraint_column_id FROM sys.foreign_key_columns k JOIN sys.columns rc ON rc.object_id = k.referenced_object_id AND rc.column_id = k.referenced_column_id @@ -116,6 +117,11 @@ export const foreignKeysRead = ` JOIN sys.foreign_keys fk ON fk.object_id = k.constraint_object_id JOIN sys.objects ro ON ro.object_id = fk.referenced_object_id JOIN sys.objects po ON po.object_id = fk.parent_object_id + JOIN sys.foreign_key_columns fkc ON fkc.parent_object_id = fk.parent_object_id and fkc.parent_column_id = k.parent_column_id + ORDER BY + po.object_id, + k.constraint_object_id, + fkc.constraint_column_id `; /**