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

Commit d3b0388e by Mick Hansen

test case and fix for N:M schema support

1 parent b68e5345
......@@ -29,8 +29,10 @@ module.exports = (function() {
foreignPrimary = foreignPrimary.length === 1 ? foreignPrimary[0] : 'id'
where[through.tableName+"."+self.association.identifier] = self.instance[instancePrimaryKey]
where[through.tableName+"."+self.association.foreignIdentifier] = {join: self.association.target.tableName+"."+foreignPrimary}
where[self.QueryInterface.quoteIdentifiers(through.getTableName())+"."+self.association.identifier] = self.instance[instancePrimaryKey]
where[self.QueryInterface.quoteIdentifiers(through.getTableName())+"."+self.association.foreignIdentifier] = {
join: self.QueryInterface.quoteIdentifiers(self.association.target.getTableName())+"."+foreignPrimary
}
if (Object(targetAssociation.through) === targetAssociation.through) {
queryOptions.hasJoinTableModel = true
......@@ -38,21 +40,21 @@ module.exports = (function() {
if (!options.attributes) {
options.attributes = [
self.QueryInterface.quoteIdentifier(self.association.target.tableName)+".*"
self.QueryInterface.quoteIdentifiers(self.association.target.getTableName())+".*"
]
}
if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function (elem) {
options.attributes.push(
self.QueryInterface.quoteIdentifiers(through.tableName + '.' + elem) + ' as ' +
self.QueryInterface.quoteIdentifiers(through.getTableName() + '.' + elem) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + elem, true)
)
})
} else {
Utils._.forOwn(through.rawAttributes, function (elem, key) {
options.attributes.push(
self.QueryInterface.quoteIdentifiers(through.tableName + '.' + key) + ' as ' +
self.QueryInterface.quoteIdentifiers(through.getTableName() + '.' + key) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + key, true)
)
})
......@@ -77,7 +79,7 @@ module.exports = (function() {
options.where = where;
}
self.association.target.findAllJoin(through.tableName, options, queryOptions)
self.association.target.findAllJoin(through.getTableName(), options, queryOptions)
.on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('error', function(err){ customEventEmitter.emit('error', err) })
.on('sql', function(sql) { customEventEmitter.emit('sql', sql)})
......
......@@ -188,10 +188,6 @@ module.exports = (function() {
this.through.rawAttributes = Utils._.merge(this.through.rawAttributes, combinedTableAttributes)
this.through.init(this.through.daoFactoryManager)
if (this.options.syncOnAssociation) {
this.through.sync()
}
} else {
var newAttributes = {}
newAttributes[this.identifier] = { type: this.options.keyType || this.target.rawAttributes[this.target.primaryKeyAttribute].type }
......@@ -282,7 +278,7 @@ module.exports = (function() {
, where = {}
where[newAssociatedObject.Model.tableName+'.'+primaryKey] = newAssociatedObject[primaryKey]
where[newAssociatedObject.Model.getTableName()+'.'+primaryKey] = newAssociatedObject[primaryKey]
return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]({ where: where })
.proxy(emitter, {events: ['error', 'sql']})
......
......@@ -472,8 +472,7 @@ module.exports = (function() {
//right now, the caller (has-many-double-linked) is in charge of the where clause
DAOFactory.prototype.findAllJoin = function(joinTableName, options, queryOptions) {
var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [this.QueryInterface.quoteIdentifier(this.tableName)+".*"]
optcpy.attributes = optcpy.attributes || [this.QueryInterface.quoteIdentifier(this.getTableName())+".*"]
// whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || null;
......
......@@ -89,7 +89,9 @@ module.exports = (function() {
} else {
onSuccess()
}
}).error(onError)
}).error(onError).on('sql', function (sql) {
self.eventEmitter.emit('sql', sql)
})
}
} else {
self.wasRunning = true
......
......@@ -75,6 +75,31 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
})
it('supports schemas', function (done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }).schema('archive')
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive')
, self = this
Task.belongsTo(User)
self.sequelize.dropAllSchemas().done(function() {
self.sequelize.createSchema('archive').done(function () {
self.sequelize.sync({force: true}).done(function () {
User.create({ username: 'foo', gender: 'male' }).success(function(user) {
Task.create({ title: 'task', status: 'inactive' }).success(function(task) {
task.setUserXYZ(user).success(function() {
task.getUserXYZ().success(function(user) {
expect(user).to.be.ok
done()
})
})
})
})
})
})
})
})
})
describe('setAssociation', function() {
......
......@@ -542,7 +542,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
describe('(N:M)', function() {
describe("getting assocations with options", function() {
describe("getAssociations", function() {
beforeEach(function(done) {
var self = this
......@@ -552,8 +552,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.User.hasMany(self.Task)
self.Task.hasMany(self.User)
this.User.sync({ force: true }).success(function() {
self.Task.sync({ force: true }).success(function() {
this.sequelize.sync({force: true}).done(function(err) {
var chainer = new Sequelize.Utils.QueryChainer([
self.User.create({ username: 'John'}),
self.Task.create({ title: 'Get rich', active: true}),
......@@ -567,7 +566,6 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
......@@ -634,6 +632,52 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done();
})
})
it('should support schemas', function (done) {
var self = this
, AcmeUser = self.sequelize.define('User', {
username: DataTypes.STRING
}).schema('acme', '_')
, AcmeProject = self.sequelize.define('Project', {
title: DataTypes.STRING, active: DataTypes.BOOLEAN
}).schema('acme', '_')
, AcmeProjectUsers = self.sequelize.define('ProjectUsers', {
status: DataTypes.STRING,
data: DataTypes.INTEGER
}).schema('acme', '_')
AcmeUser.hasMany(AcmeProject, {through: AcmeProjectUsers})
AcmeProject.hasMany(AcmeUser, {through: AcmeProjectUsers})
self.sequelize.dropAllSchemas().done(function(err) {
expect(err).not.to.be.ok
self.sequelize.createSchema('acme').done(function(err) {
expect(err).not.to.be.ok
self.sequelize.sync({force: true}).done(function(err) {
expect(err).not.to.be.ok
AcmeUser.create().done(function(err, u) {
expect(err).not.to.be.ok
AcmeProject.create().done(function(err, p) {
expect(err).not.to.be.ok
u.addProject(p, { status: 'active', data: 42 }).done(function(err) {
expect(err).not.to.be.ok
u.getProjects().done(function(err, projects) {
expect(err).not.to.be.ok
expect(projects).to.have.length(1);
var project = projects[0]
expect(project.ProjectUsers).to.be.defined
expect(project.status).not.to.exist
expect(project.ProjectUsers.status).to.equal('active')
done()
})
})
})
})
})
})
})
})
})
it("removes the reference id, which was added in the first place", function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!