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

Commit 59497ddf by Daniel Durante

Fixes wrong table name being referenced with the schema option. Closes https://g…

…ithub.com/sequelize/sequelize/issues/843
1 parent 3e534a9b
...@@ -13,10 +13,21 @@ module.exports = { ...@@ -13,10 +13,21 @@ module.exports = {
return source.rawAttributes[key].primaryKey return source.rawAttributes[key].primaryKey
}) })
if(primaryKeys.length == 1) { if (primaryKeys.length === 1) {
newAttribute.references = source.tableName, if (!!source.options.schema) {
newAttribute.references = source.daoFactoryManager.sequelize.queryInterface.QueryGenerator.addSchema({
tableName: source.tableName,
options: {
schema: source.options.schema,
schemaDelimiter: source.options.schemaDelimiter
}
})
} else {
newAttribute.references = source.tableName
}
newAttribute.referencesKey = primaryKeys[0] newAttribute.referencesKey = primaryKeys[0]
newAttribute.onDelete = options.onDelete, newAttribute.onDelete = options.onDelete
newAttribute.onUpdate = options.onUpdate newAttribute.onUpdate = options.onUpdate
} }
} }
......
...@@ -15,7 +15,7 @@ module.exports = (function() { ...@@ -15,7 +15,7 @@ module.exports = (function() {
var schema = (!!opts.options && !!opts.options.schema ? opts.options.schema : undefined) var schema = (!!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined) var schemaDelimiter = (!!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
if (!!opts.tableName) { if (!!opts && !!opts.tableName) {
tableName = opts.tableName tableName = opts.tableName
} }
else if (typeof opts === "string") { else if (typeof opts === "string") {
...@@ -26,7 +26,7 @@ module.exports = (function() { ...@@ -26,7 +26,7 @@ module.exports = (function() {
return tableName return tableName
} }
return this.quoteIdentifier(schema) + '.' + this.quoteIdentifier(tableName) return this.quoteIdentifiers((!!schema ? (schema + '.' + tableName) : tableName));
}, },
createSchema: function(schema) { createSchema: function(schema) {
...@@ -665,10 +665,10 @@ module.exports = (function() { ...@@ -665,10 +665,10 @@ module.exports = (function() {
if(dataType.references) { if(dataType.references) {
template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)" template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements.referencesTable = this.quoteIdentifier(dataType.references) replacements.referencesTable = this.quoteIdentifiers(dataType.references)
if(dataType.referencesKey) { if(dataType.referencesKey) {
replacements.referencesKey = this.quoteIdentifier(dataType.referencesKey) replacements.referencesKey = this.quoteIdentifiers(dataType.referencesKey)
} else { } else {
replacements.referencesKey = this.quoteIdentifier('id') replacements.referencesKey = this.quoteIdentifier('id')
} }
......
...@@ -592,7 +592,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -592,7 +592,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
this.User.hasMany(this.Project, { joinTableModel: this.UserProjects }) this.User.hasMany(this.Project, { joinTableModel: this.UserProjects })
this.Project.hasMany(this.User, { joinTableModel: this.UserProjects }) this.Project.hasMany(this.User, { joinTableModel: this.UserProjects })
this.sequelize.sync().success(function() { done() }) this.sequelize.sync().success(function() { done() })
}) })
...@@ -614,7 +614,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -614,7 +614,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done() done()
}) })
}) })
}) })
}) })
}) })
...@@ -635,13 +635,13 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -635,13 +635,13 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done() done()
}) })
}) })
}) })
}) })
}) })
}) })
describe('inserting in join table', function () { describe('inserting in join table', function () {
describe('add', function () { describe('add', function () {
it('should insert data provided on the object into the join table', function (done) { it('should insert data provided on the object into the join table', function (done) {
...@@ -658,7 +658,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -658,7 +658,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done() done()
}) })
}) })
}) })
}) })
}) })
...@@ -672,7 +672,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -672,7 +672,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done() done()
}) })
}) })
}) })
}) })
}) })
}) })
...@@ -704,7 +704,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -704,7 +704,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
}) })
}) })
}) })
}) })
......
...@@ -3491,6 +3491,44 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -3491,6 +3491,44 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('should be able to reference a table with a schema set', function(done) {
var self = this
var sequelize = this.sequelize
var UserPub = this.sequelize.define('UserPub', {
username: Sequelize.STRING
}, { schema: 'prefix' })
var ItemPub = this.sequelize.define('ItemPub', {
name: Sequelize.STRING
}, { schema: 'prefix' })
UserPub.hasMany(ItemPub, {
foreignKeyConstraint: true
})
var run = function() {
UserPub.sync({ force: true }).success(function() {
ItemPub.sync({ force: true }).on('sql', function(sql) {
if (dialect === "postgres") {
expect(sql).to.match(/REFERENCES\s+"prefix"\."UserPubs" \("id"\)/)
} else {
expect(sql).to.match(/REFERENCES\s+`prefix\.UserPubs` \(`id`\)/)
}
done()
})
})
}
if (dialect === "postgres") {
this.sequelize.queryInterface.createSchema('prefix').success(function() {
run.call(self)
})
} else {
run.call(self)
}
})
it("should be able to create and update records under any valid schematic", function(done){ it("should be able to create and update records under any valid schematic", function(done){
var self = this var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!