Skip to content

Commit

Permalink
fix: updated constraint column order (#16614)
Browse files Browse the repository at this point in the history
  • Loading branch information
lohart13 committed Oct 10, 2023
1 parent 399b4b3 commit dd2aa18
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Db2QueryGeneratorTypeScript extends AbstractQueryGenerator {
options?.columnName ? `AND k.COLNAME = ${this.escape(options.columnName)}` : '',
options?.constraintName ? `AND c.CONSTNAME = ${this.escape(options.constraintName)}` : '',
options?.constraintType ? `AND c.TYPE = ${this.escape(this._getConstraintType(options.constraintType))}` : '',
'ORDER BY c.CONSTNAME, k.COLSEQ',
'ORDER BY c.CONSTNAME, k.COLSEQ, fk.COLSEQ',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class IBMiQueryGeneratorTypeScript extends AbstractQueryGenerator {
options?.columnName ? `AND k.COLUMN_NAME = ${this.escape(options.columnName)}` : '',
options?.constraintName ? `AND c.CONSTRAINT_NAME = ${this.escape(options.constraintName)}` : '',
options?.constraintType ? `AND c.CONSTRAINT_TYPE = ${this.escape(options.constraintType)}` : '',
'ORDER BY c.CONSTRAINT_NAME, k.ORDINAL_POSITION',
'ORDER BY c.CONSTRAINT_NAME, k.ORDINAL_POSITION, fk.ORDINAL_POSITION',
]);
}

Expand Down
64 changes: 31 additions & 33 deletions packages/core/src/dialects/mssql/query-generator-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,43 +183,41 @@ export class MsSqlQueryGeneratorTypeScript extends AbstractQueryGenerator {
const table = this.extractTableDetails(tableName);

return joinSQLFragments([
'SELECT DB_NAME() AS constraintCatalog,',
's.[name] AS constraintSchema,',
'c.constraintName,',
`REPLACE(LEFT(c.constraintType, CHARINDEX('_CONSTRAINT', c.constraintType) - 1), '_', ' ') AS constraintType,`,
'DB_NAME() AS tableCatalog,',
's.[name] AS tableSchema,',
't.[name] AS tableName,',
'c.columnNames,',
'c.referencedTableSchema,',
'c.referencedTableName,',
'c.referencedColumnNames,',
'c.deleteAction,',
'c.updateAction,',
'c.definition',
'FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id',
'INNER JOIN (',
'SELECT kc.[name] AS constraintName, kc.[type_desc] AS constraintType, kc.[parent_object_id] AS constraintTableId, c.[name] AS columnNames, null as referencedTableSchema, null AS referencedTableName',
', null AS referencedColumnNames, null AS deleteAction, null AS updateAction, null AS definition',
'FROM sys.key_constraints kc LEFT JOIN sys.indexes i ON kc.name = i.name',
'LEFT JOIN sys.index_columns ic ON ic.index_id = i.index_id AND ic.object_id = kc.parent_object_id',
'LEFT JOIN sys.columns c ON c.column_id = ic.column_id AND c.object_id = kc.parent_object_id UNION ALL',
'SELECT [name] AS constraintName, [type_desc] AS constraintType, [parent_object_id] AS constraintTableId, null AS columnNames, null as referencedTableSchema, null AS referencedTableName',
', null AS referencedColumnNames, null AS deleteAction, null AS updateAction, [definition] FROM sys.check_constraints c UNION ALL',
'SELECT dc.[name] AS constraintName, dc.[type_desc] AS constraintType, dc.[parent_object_id] AS constraintTableId, c.[name] AS columnNames, null as referencedTableSchema, null AS referencedTableName',
', null AS referencedColumnNames, null AS deleteAction, null AS updateAction, [definition] FROM sys.default_constraints dc',
'INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id UNION ALL',
'SELECT k.[name] AS constraintName, k.[type_desc] AS constraintType, k.[parent_object_id] AS constraintTableId, fcol.[name] AS columnNames, OBJECT_SCHEMA_NAME(k.[referenced_object_id]) as referencedTableSchema',
', OBJECT_NAME(k.[referenced_object_id]) AS referencedTableName, rcol.[name] AS referencedColumnNames, k.[delete_referential_action_desc] AS deleteAction',
', k.[update_referential_action_desc] AS updateAction, null AS definition FROM sys.foreign_keys k INNER JOIN sys.foreign_key_columns c ON k.[object_id] = c.constraint_object_id',
'INNER JOIN sys.columns fcol ON c.parent_column_id = fcol.column_id AND c.parent_object_id = fcol.object_id',
'INNER JOIN sys.columns rcol ON c.referenced_column_id = rcol.column_id AND c.referenced_object_id = rcol.object_id',
') c ON t.object_id = c.constraintTableId',
`SELECT DB_NAME() AS constraintCatalog`,
`, s.[name] AS constraintSchema`,
`, c.constraintName`,
`, REPLACE(LEFT(c.constraintType, CHARINDEX('_CONSTRAINT', c.constraintType) - 1), '_', ' ') AS constraintType`,
`, DB_NAME() AS tableCatalog`,
`, s.[name] AS tableSchema`,
`, t.[name] AS tableName`,
`, c.columnNames`,
`, c.referencedTableSchema`,
`, c.referencedTableName`,
`, c.referencedColumnNames`,
`, c.deleteAction`,
`, c.updateAction`,
`, c.definition`,
`FROM sys.tables t`,
`INNER JOIN sys.schemas s ON t.schema_id = s.schema_id`,
`INNER JOIN (`,
`SELECT kc.[name] AS constraintName, kc.[type_desc] AS constraintType, kc.[parent_object_id] AS constraintTableId, c.[name] AS columnNames, null as referencedTableSchema`,
`, null AS referencedTableName, null AS referencedColumnNames, null AS deleteAction, null AS updateAction, null AS [definition], null AS column_id FROM sys.key_constraints kc`,
`LEFT JOIN sys.indexes i ON kc.name = i.name LEFT JOIN sys.index_columns ic ON ic.index_id = i.index_id AND ic.object_id = kc.parent_object_id LEFT JOIN sys.columns c ON c.column_id = ic.column_id AND c.object_id = kc.parent_object_id`,
`UNION ALL SELECT [name] AS constraintName, [type_desc] AS constraintType, [parent_object_id] AS constraintTableId, null AS columnNames, null as referencedTableSchema, null AS referencedTableName`,
`, null AS referencedColumnNames, null AS deleteAction, null AS updateAction, [definition], null AS column_id FROM sys.check_constraints c UNION ALL`,
`SELECT dc.[name] AS constraintName, dc.[type_desc] AS constraintType, dc.[parent_object_id] AS constraintTableId, c.[name] AS columnNames, null as referencedTableSchema`,
`, null AS referencedTableName, null AS referencedColumnNames, null AS deleteAction, null AS updateAction, [definition], null AS column_id FROM sys.default_constraints dc`,
`INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id UNION ALL`,
`SELECT k.[name] AS constraintName, k.[type_desc] AS constraintType, k.[parent_object_id] AS constraintTableId, fcol.[name] AS columnNames, OBJECT_SCHEMA_NAME(k.[referenced_object_id]) as referencedTableSchema`,
`, OBJECT_NAME(k.[referenced_object_id]) AS referencedTableName, rcol.[name] AS referencedColumnNames, k.[delete_referential_action_desc] AS deleteAction, k.[update_referential_action_desc] AS updateAction`,
`, null AS [definition], rcol.column_id FROM sys.foreign_keys k INNER JOIN sys.foreign_key_columns c ON k.[object_id] = c.constraint_object_id`,
`INNER JOIN sys.columns fcol ON c.parent_column_id = fcol.column_id AND c.parent_object_id = fcol.object_id INNER JOIN sys.columns rcol ON c.referenced_column_id = rcol.column_id AND c.referenced_object_id = rcol.object_id`,
`) c ON t.object_id = c.constraintTableId`,
`WHERE s.name = ${this.escape(table.schema)} AND t.name = ${this.escape(table.tableName)}`,
options?.columnName ? `AND c.columnNames = ${this.escape(options.columnName)}` : '',
options?.constraintName ? `AND c.constraintName = ${this.escape(options.constraintName)}` : '',
options?.constraintType ? `AND c.constraintType = ${this.escape(this._getConstraintType(options.constraintType))}` : '',
'ORDER BY c.constraintName',
`ORDER BY c.constraintName, c.column_id`,
]);
}

Expand Down

0 comments on commit dd2aa18

Please sign in to comment.