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

Commit b0eecfa8 by Sascha Depold

fixed eager loading of 2 associations with the same origin but different alias

1 parent 08122ca4
...@@ -133,22 +133,19 @@ module.exports = (function() { ...@@ -133,22 +133,19 @@ module.exports = (function() {
options.include.forEach(function(include) { options.include.forEach(function(include) {
var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) { var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) {
var template = Utils._.template("`<%= table %>`.`<%= attr %>` AS `<%= as %>.<%= attr %>`") var template = Utils._.template("`<%= as %>`.`<%= attr %>` AS `<%= as %>.<%= attr %>`")
return template({ return template({ as: include.as, attr: attr })
table: include.daoFactory.tableName,
as: include.as,
attr: attr
})
}) })
optAttributes = optAttributes.concat(attributes) optAttributes = optAttributes.concat(attributes)
var joinQuery = " LEFT OUTER JOIN `<%= table %>` ON `<%= tableLeft %>`.`<%= attrLeft %>` = `<%= tableRight %>`.`<%= attrRight %>`" var joinQuery = " LEFT OUTER JOIN `<%= table %>` AS `<%= as %>` ON `<%= tableLeft %>`.`<%= attrLeft %>` = `<%= tableRight %>`.`<%= attrRight %>`"
query += Utils._.template(joinQuery)({ query += Utils._.template(joinQuery)({
table: include.daoFactory.tableName, table: include.daoFactory.tableName,
tableLeft: ((include.association.associationType === 'BelongsTo') ? include.daoFactory.tableName : tableName), as: include.as,
tableLeft: ((include.association.associationType === 'BelongsTo') ? include.as : tableName),
attrLeft: 'id', attrLeft: 'id',
tableRight: ((include.association.associationType === 'BelongsTo') ? tableName : include.daoFactory.tableName), tableRight: ((include.association.associationType === 'BelongsTo') ? tableName : include.as),
attrRight: include.association.identifier attrRight: include.association.identifier
}) })
}) })
......
...@@ -235,22 +235,19 @@ module.exports = (function() { ...@@ -235,22 +235,19 @@ module.exports = (function() {
options.include.forEach(function(include) { options.include.forEach(function(include) {
var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) { var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) {
var template = Utils._.template('"<%= table %>"."<%= attr %>" AS "<%= as %>.<%= attr %>"') var template = Utils._.template('"<%= as %>"."<%= attr %>" AS "<%= as %>.<%= attr %>"')
return template({ return template({ as: include.as, attr: attr })
table: include.daoFactory.tableName,
as: include.as,
attr: attr
})
}) })
optAttributes = optAttributes.concat(attributes) optAttributes = optAttributes.concat(attributes)
var joinQuery = ' LEFT OUTER JOIN "<%= table %>" ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"' var joinQuery = ' LEFT OUTER JOIN "<%= table %>" AS "<%= as %>" ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"'
query += Utils._.template(joinQuery)({ query += Utils._.template(joinQuery)({
table: include.daoFactory.tableName, table: include.daoFactory.tableName,
tableLeft: ((include.association.associationType === 'BelongsTo') ? include.daoFactory.tableName : tableName), as: include.as,
tableLeft: ((include.association.associationType === 'BelongsTo') ? include.as : tableName),
attrLeft: 'id', attrLeft: 'id',
tableRight: ((include.association.associationType === 'BelongsTo') ? tableName : include.daoFactory.tableName), tableRight: ((include.association.associationType === 'BelongsTo') ? tableName : include.as),
attrRight: include.association.identifier attrRight: include.association.identifier
}) })
}) })
......
...@@ -52,18 +52,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -52,18 +52,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}) })
it("throws an error if 2 autoIncrements are passed", function() { it("throws an error if 2 autoIncrements are passed", function() {
try { Helpers.assertException(function() {
var User = this.sequelize.define('UserWithTwoAutoIncrements', { this.sequelize.define('UserWithTwoAutoIncrements', {
userid: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, userid: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
userscore: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true } userscore: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }
}) })
// the parse shouldn't execute the following line }.bind(this), 'Invalid DAO definition. Only one autoincrement field allowed.')
// this tests needs to be refactored...
// we need to use expect.toThrow when a later version than 0.6 was released
expect(1).toEqual(2)
} catch(e) {
expect(e.message).toEqual('Invalid DAO definition. Only one autoincrement field allowed.')
}
}) })
}) })
...@@ -205,18 +199,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -205,18 +199,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}) })
}) })
it('raises an error if you mess up the datatype', function(done) { it('raises an error if you mess up the datatype', function() {
Helpers.assertException(function() {
try { this.sequelize.define('UserBadDataType', {
var User = this.sequelize.define('UserBadDataType', {
activity_date: Sequelize.DATe activity_date: Sequelize.DATe
}); })
done() }.bind(this), 'Unrecognized data type for field activity_date')
}
catch( e ) {
expect(e.message).toEqual('Unrecognized data type for field activity_date')
done()
}
}) })
it('sets a 64 bit int in bigint', function(done) { it('sets a 64 bit int in bigint', function(done) {
...@@ -468,24 +456,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -468,24 +456,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
describe('eager loading', function() { describe('eager loading', function() {
before(function() { before(function() {
this.Task = this.sequelize.define('Task', { title: Sequelize.STRING }) this.Task = this.sequelize.define('Task', { title: Sequelize.STRING })
this.Worker = this.sequelize.define('Worker', { name: Sequelize.STRING }) this.Worker = this.sequelize.define('Worker', { name: Sequelize.STRING })
})
describe('belongsTo', function() {
before(function(done) {
this.Task.belongsTo(this.Worker)
this.init = function(callback) {
this.sequelize.sync({ force: true }).complete(function() { this.sequelize.sync({ force: true }).complete(function() {
this.Worker.create({ name: 'worker' }).success(function(worker) { this.Worker.create({ name: 'worker' }).success(function(worker) {
this.Task.create({ title: 'homework' }).success(function(task) { this.Task.create({ title: 'homework' }).success(function(task) {
this.worker = worker this.worker = worker
this.task = task this.task = task
this.task.setWorker(this.worker).success(done) callback()
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
}.bind(this)
})
describe('belongsTo', function() {
before(function(done) {
this.Task.belongsTo(this.Worker)
this.init(function() {
this.task.setWorker(this.worker).success(done)
}.bind(this))
}) })
it('throws an error about unexpected input if include contains a non-object', function() { it('throws an error about unexpected input if include contains a non-object', function() {
...@@ -518,6 +511,43 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -518,6 +511,43 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
done() done()
}.bind(this)) }.bind(this))
}) })
it('returns the private and public ip', function(done) {
var Domain = this.sequelize.define('Domain', { ip: Sequelize.STRING })
var Environment = this.sequelize.define('Environment', { name: Sequelize.STRING })
Environment
.belongsTo(Domain, { as: 'PrivateDomain', foreignKey: 'privateDomainId' })
.belongsTo(Domain, { as: 'PublicDomain', foreignKey: 'publicDomainId' })
this.sequelize.sync({ force: true }).complete(function() {
Domain.create({ ip: '192.168.0.1' }).success(function(privateIp) {
Domain.create({ ip: '91.65.189.19' }).success(function(publicIp) {
Environment.create({ name: 'environment' }).success(function(env) {
env.setPrivateDomain(privateIp).success(function() {
env.setPublicDomain(publicIp).success(function() {
Environment.find({
where: { name: 'environment' },
include: [
{ daoFactory: Domain, as: 'PrivateDomain' },
{ daoFactory: Domain, as: 'PublicDomain' }
]
}).complete(function(err, environment) {
expect(err).toBeNull()
expect(environment).toBeDefined()
expect(environment.privateDomain).toBeDefined()
expect(environment.privateDomain.ip).toEqual('192.168.0.1')
expect(environment.publicDomain).toBeDefined()
expect(environment.publicDomain.ip).toEqual('91.65.189.19')
done()
})
})
})
})
})
})
})
})
}) })
describe('hasOne', function() { describe('hasOne', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!