Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postgres): schema for index and relations #11274

Merged
merged 3 commits into from Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/dialects/abstract/query-generator.js
Expand Up @@ -569,6 +569,13 @@ class QueryGenerator {
options.where = this.whereQuery(options.where);
}

if (options.schema) {
tableName = {
tableName,
schema: options.schema
};
}

if (typeof tableName === 'string') {
tableName = this.quoteIdentifiers(tableName);
} else {
Expand Down
16 changes: 14 additions & 2 deletions lib/dialects/postgres/query-generator.js
Expand Up @@ -514,7 +514,19 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}

if (attribute.references) {
const referencesTable = this.quoteTable(attribute.references.model);
let referencesTable = this.quoteTable(attribute.references.model);
const tableDetails = this.extractTableDetails(referencesTable, options);
let schema;
if (options.schema) {
schema = options.schema;
} else if ((!attribute.references.model || typeof attribute.references.model == 'string')
&& options.table && options.table.schema) {
schema = options.table.schema;
}

if (schema) {
referencesTable = schema + tableDetails.delimiter + referencesTable;
}
let referencesKey;

if (attribute.references.key) {
Expand Down Expand Up @@ -609,7 +621,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {

const paramList = this.expandFunctionParamList(params);
const expandedOptionsArray = this.expandOptions(optionsArray);

const statement = options && options.force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION';

return `${statement} ${functionName}(${paramList}) RETURNS ${returnType} AS $func$ BEGIN ${body} END; $func$ language '${language}'${expandedOptionsArray};`;
Expand Down
3 changes: 2 additions & 1 deletion lib/model.js
Expand Up @@ -1362,7 +1362,8 @@ class Model {
Object.assign({
logging: options.logging,
benchmark: options.benchmark,
transaction: options.transaction
transaction: options.transaction,
schema: options.schema
}, index),
this.tableName
));
Expand Down
33 changes: 33 additions & 0 deletions test/integration/dialects/postgres/associations.test.js
Expand Up @@ -140,6 +140,39 @@ if (dialect.match(/^postgres/)) {
});
});
});
it('defaults to schema provided to sync() for references #11276', function() {
const User = this.sequelize.define('UserXYZ', {
uid: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
}
}),
Task = this.sequelize.define('TaskXYZ', {
});

Task.belongsTo(User);

return Support.dropTestSchemas(this.sequelize).then(() => {
return this.sequelize.createSchema('archive');
}).then(() => {
return User.sync({ force: true, schema: 'archive' });
}).then(() => {
return Task.sync({ force: true, schema: 'archive' });
}).then(() => {
return User.schema('archive').create({});
}).then(user => {
return Task.schema('archive').create({}).then(task => {
return task.setUserXYZ(user).then(() => {
return task.getUserXYZ({ schema: 'archive' });
});
});
}).then(user => {
expect(user).to.be.ok;
return this.sequelize.dropSchema('archive');
});
});
});
});
});
Expand Down