Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Fix fk text cols #135

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/generators/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/queries/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,20 @@ 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
JOIN sys.columns c ON c.object_id = k.parent_object_id AND c.column_id = k.parent_column_id
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
`;

/**
Expand Down