不要怂,就是干,撸起袖子干!

Commit 48160056 by Thomas Hoppe Committed by Sushant

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

1 parent d5667e03
......@@ -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 {
......
......@@ -514,7 +514,24 @@ 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) {
......@@ -610,6 +627,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
const paramList = this.expandFunctionParamList(params);
const variableList = options && options.variables ? this.expandFunctionVariableList(options.variables) : '';
const expandedOptionsArray = this.expandOptions(optionsArray);
const statement = options && options.force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION';
return `${statement} ${functionName}(${paramList}) RETURNS ${returnType} AS $func$ ${variableList} BEGIN ${body} END; $func$ language '${language}'${expandedOptionsArray};`;
......
......@@ -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
));
......
......@@ -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');
});
});
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!