Skip to content

Commit

Permalink
Fixes wrong table name being referenced with the schema option. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
durango committed Oct 1, 2013
1 parent 3e534a9 commit 59497dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
17 changes: 14 additions & 3 deletions lib/associations/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ module.exports = {
return source.rawAttributes[key].primaryKey
})

if(primaryKeys.length == 1) {
newAttribute.references = source.tableName,
if (primaryKeys.length === 1) {
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.onDelete = options.onDelete,
newAttribute.onDelete = options.onDelete
newAttribute.onUpdate = options.onUpdate
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (function() {
var schema = (!!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)

if (!!opts.tableName) {
if (!!opts && !!opts.tableName) {
tableName = opts.tableName
}
else if (typeof opts === "string") {
Expand All @@ -26,7 +26,7 @@ module.exports = (function() {
return tableName
}

return this.quoteIdentifier(schema) + '.' + this.quoteIdentifier(tableName)
return this.quoteIdentifiers((!!schema ? (schema + '.' + tableName) : tableName));
},

createSchema: function(schema) {
Expand Down Expand Up @@ -665,10 +665,10 @@ module.exports = (function() {

if(dataType.references) {
template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements.referencesTable = this.quoteIdentifier(dataType.references)
replacements.referencesTable = this.quoteIdentifiers(dataType.references)

if(dataType.referencesKey) {
replacements.referencesKey = this.quoteIdentifier(dataType.referencesKey)
replacements.referencesKey = this.quoteIdentifiers(dataType.referencesKey)
} else {
replacements.referencesKey = this.quoteIdentifier('id')
}
Expand Down
14 changes: 7 additions & 7 deletions test/associations/has-many.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {

this.User.hasMany(this.Project, { joinTableModel: this.UserProjects })
this.Project.hasMany(this.User, { joinTableModel: this.UserProjects })

this.sequelize.sync().success(function() { done() })
})

Expand All @@ -614,7 +614,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done()
})
})
})
})
})
})

Expand All @@ -635,13 +635,13 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done()
})
})
})
})
})
})
})

describe('inserting in join table', function () {


describe('add', function () {
it('should insert data provided on the object into the join table', function (done) {
Expand All @@ -658,7 +658,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done()
})
})
})
})
})
})

Expand All @@ -672,7 +672,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done()
})
})
})
})
})
})
})
Expand Down Expand Up @@ -704,7 +704,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
})
})
})
})
Expand Down
38 changes: 38 additions & 0 deletions test/dao-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
var self = this

Expand Down

0 comments on commit 59497dd

Please sign in to comment.