Skip to content

Commit

Permalink
fix(postgres): filter also on schema name when retrieving foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ghusse committed Nov 16, 2023
1 parent 252e6d2 commit d8c1be1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,12 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
'FROM information_schema.table_constraints AS tc ' +
'JOIN information_schema.key_column_usage AS kcu ' +
'ON tc.constraint_name = kcu.constraint_name ' +
'AND tc.table_schema = kcu.table_schema ' +
'AND tc.table_catalog = kcu.table_catalog ' +
'JOIN information_schema.constraint_column_usage AS ccu ' +
'ON ccu.constraint_name = tc.constraint_name ';
'ON ccu.constraint_name = tc.constraint_name ' +
'AND ccu.table_schema = tc.table_schema ' +
'AND ccu.table_catalog = tc.table_catalog ';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/dialects/postgres/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class PostgresQueryInterface extends QueryInterface {

// postgres needs some special treatment as those field names returned are all lowercase
// in order to keep same result with other dialects.
const query = this.queryGenerator.getForeignKeyReferencesQuery(table.tableName || table, this.sequelize.config.database);
const query = this.queryGenerator.getForeignKeyReferencesQuery(table.tableName || table, this.sequelize.config.database, table.schema);
const result = await this.sequelize.query(query, queryOptions);

return result.map(fkMeta => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,50 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(refs[0]).deep.include.all(expectedObject);

});

describe('with schemas', () => {
if (Support.sequelize.dialect.supports.schemas) {
beforeEach(async function() {
this.schema = 'test_schema';
await this.queryInterface.createSchema(this.schema);

const TaskTest = this.sequelize.define('TaskTest', { title: DataTypes.STRING }, { tableName: 'Tasks', schema: this.schema });
const UserTest = this.sequelize.define('UserTest', { username: DataTypes.STRING }, { tableName: 'Users', schema: this.schema });

UserTest.hasOne(TaskTest, { foreignKey: 'UserId' });

const Task = this.sequelize.define('Task', { title: DataTypes.STRING }, { tableName: 'Tasks' });
const User = this.sequelize.define('User', { username: DataTypes.STRING }, { tableName: 'Users' });

User.hasOne(Task, { foreignKey: 'UserId' });

await UserTest.sync({ force: true });
await TaskTest.sync({ force: true });
await User.sync({ force: true });
await Task.sync({ force: true });
});

it('should return only references to the tables on the right schema', async function() {
const tasksTestRefs = await this.queryInterface.getForeignKeyReferencesForTable({ tableName: 'Tasks', schema: this.schema });
expect(tasksTestRefs).to.have.lengthOf(1);
expect(tasksTestRefs[0]).deep.include.all({
columnName: 'UserId',
referencedColumnName: 'id',
referencedTableName: 'Users',
tableSchema: this.schema
});
});

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({
columnName: 'UserId',
referencedColumnName: 'id',
referencedTableName: 'Users'
});
});
}
});
});
});

0 comments on commit d8c1be1

Please sign in to comment.