Skip to content

Commit

Permalink
fix: use the default schema when not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ghusse committed Nov 16, 2023
1 parent 884c001 commit ba945ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/dialects/mssql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
let sql = `${this._getForeignKeysQueryPrefix(catalogName)
} WHERE TB.NAME =${wrapSingleQuote(tableName)}`;

if (table.schema) {
sql += ` AND SCHEMA_NAME(TB.SCHEMA_ID) =${wrapSingleQuote(table.schema)}`;
}
sql += ` AND SCHEMA_NAME(TB.SCHEMA_ID) =${wrapSingleQuote(table.schema || 'dbo')}`;

return sql;
}

Expand Down
12 changes: 8 additions & 4 deletions src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,18 +904,22 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
* @param {string} schemaName
*/
getForeignKeyReferencesQuery(tableName, catalogName, schemaName) {
const schema = schemaName || 'public';

return `${this._getForeignKeyReferencesQueryPrefix()
}WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '${tableName}'${
catalogName ? ` AND tc.table_catalog = '${catalogName}'` : ''
}${schemaName ? ` AND tc.table_schema = '${schemaName}'` : ''}`;
} AND tc.table_schema = '${schema}'`;
}

getForeignKeyReferenceQuery(table, columnName) {
const tableName = table.tableName || table;
const schema = table.schema;
const schema = table.schema || 'public';
return `${this._getForeignKeyReferencesQueryPrefix()
}WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='${tableName}' AND kcu.column_name = '${columnName}'${
schema ? ` AND tc.table_schema = '${schema}'` : ''}`;
}WHERE constraint_type = 'FOREIGN KEY'
AND tc.table_name='${tableName}'
AND kcu.column_name = '${columnName}'
AND tc.table_schema = '${schema}'`;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DataTypes = require('../../../lib/data-types');

describe(Support.getTestDialectTeaser('QueryInterface'), () => {
beforeEach(function() {
this.sequelize.options.quoteIdenifiers = true;
this.sequelize.options.quoteIdentifiers = true;
this.queryInterface = this.sequelize.getQueryInterface();
});

Expand Down Expand Up @@ -42,7 +42,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});

describe('with schemas', () => {
if (Support.sequelize.dialect.supports.schemas) {
// MariaDB does not really support schemas (they are synonyms of databases)
if (Support.sequelize.dialect.supports.schemas && Support.getTestDialect() !== 'mariadb') {
beforeEach(async function() {
this.schema = 'test_schema';
await this.queryInterface.createSchema(this.schema);
Expand Down Expand Up @@ -77,7 +78,7 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
it('should only return references to the tables on the default schema', async function() {
const taskRefs = await this.queryInterface.getForeignKeyReferencesForTable('Tasks');
expect(taskRefs).to.have.lengthOf(1);
expect(taskRefs[0]).deep.equal({
expect(taskRefs[0]).deep.include.all({
columnName: 'UserId',
referencedColumnName: 'id',
referencedTableName: 'Users'
Expand Down

0 comments on commit ba945ef

Please sign in to comment.