Skip to content

Commit 4816005

Browse files
vanthomesushantdhiman
authored andcommitted
fix(postgres): use proper schema for index and relations (#11274)
1 parent d5667e0 commit 4816005

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

lib/dialects/abstract/query-generator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,13 @@ class QueryGenerator {
569569
options.where = this.whereQuery(options.where);
570570
}
571571

572+
if (options.schema) {
573+
tableName = {
574+
tableName,
575+
schema: options.schema
576+
};
577+
}
578+
572579
if (typeof tableName === 'string') {
573580
tableName = this.quoteIdentifiers(tableName);
574581
} else {

lib/dialects/postgres/query-generator.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,24 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
514514
}
515515

516516
if (attribute.references) {
517-
const referencesTable = this.quoteTable(attribute.references.model);
517+
let referencesTable = this.quoteTable(attribute.references.model);
518+
const tableDetails = this.extractTableDetails(referencesTable, options);
519+
let schema;
520+
521+
if (options.schema) {
522+
schema = options.schema;
523+
} else if (
524+
(!attribute.references.model || typeof attribute.references.model == 'string')
525+
&& options.table
526+
&& options.table.schema
527+
) {
528+
schema = options.table.schema;
529+
}
530+
531+
if (schema) {
532+
referencesTable = schema + tableDetails.delimiter + referencesTable;
533+
}
534+
518535
let referencesKey;
519536

520537
if (attribute.references.key) {
@@ -610,6 +627,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
610627
const paramList = this.expandFunctionParamList(params);
611628
const variableList = options && options.variables ? this.expandFunctionVariableList(options.variables) : '';
612629
const expandedOptionsArray = this.expandOptions(optionsArray);
630+
613631
const statement = options && options.force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION';
614632

615633
return `${statement} ${functionName}(${paramList}) RETURNS ${returnType} AS $func$ ${variableList} BEGIN ${body} END; $func$ language '${language}'${expandedOptionsArray};`;

lib/model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,8 @@ class Model {
13621362
Object.assign({
13631363
logging: options.logging,
13641364
benchmark: options.benchmark,
1365-
transaction: options.transaction
1365+
transaction: options.transaction,
1366+
schema: options.schema
13661367
}, index),
13671368
this.tableName
13681369
));

test/integration/dialects/postgres/associations.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,39 @@ if (dialect.match(/^postgres/)) {
140140
});
141141
});
142142
});
143+
it('defaults to schema provided to sync() for references #11276', function() {
144+
const User = this.sequelize.define('UserXYZ', {
145+
uid: {
146+
type: DataTypes.INTEGER,
147+
primaryKey: true,
148+
autoIncrement: true,
149+
allowNull: false
150+
}
151+
}),
152+
Task = this.sequelize.define('TaskXYZ', {
153+
});
154+
155+
Task.belongsTo(User);
156+
157+
return Support.dropTestSchemas(this.sequelize).then(() => {
158+
return this.sequelize.createSchema('archive');
159+
}).then(() => {
160+
return User.sync({ force: true, schema: 'archive' });
161+
}).then(() => {
162+
return Task.sync({ force: true, schema: 'archive' });
163+
}).then(() => {
164+
return User.schema('archive').create({});
165+
}).then(user => {
166+
return Task.schema('archive').create({}).then(task => {
167+
return task.setUserXYZ(user).then(() => {
168+
return task.getUserXYZ({ schema: 'archive' });
169+
});
170+
});
171+
}).then(user => {
172+
expect(user).to.be.ok;
173+
return this.sequelize.dropSchema('archive');
174+
});
175+
});
143176
});
144177
});
145178
});

0 commit comments

Comments
 (0)