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

Commit a9bc35d2 by Jan Aagaard Meier

Allow multiple hasmany associations of the same model

1 parent 21fbae6d
......@@ -18,8 +18,8 @@ module.exports = (function() {
this.source.tableName,
this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName
)
this.associationAccessor = this.combinedName = (this.options.joinTableName || combinedTableName)
this.options.tableName = this.combinedName
this.options.tableName = this.combinedName = (this.options.joinTableName || combinedTableName)
this.associationAccessor = this.options.as || this.combinedName
var as = (this.options.as || Utils.pluralize(this.target.tableName, this.target.options.language))
......
/* jshint camelcase: false */
/* jshint camelcase: false, expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
......@@ -7,6 +7,18 @@ var chai = require('chai')
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("BelongsTo"), function() {
describe("Model.associations", function () {
it("should store all assocations when associting to the same table multiple times", function () {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
Group.belongsTo(User, { foreignKey: 'primaryGroupId', as: 'primaryUsers' });
Group.belongsTo(User, { foreignKey: 'secondaryGroupId', as: 'secondaryUsers' });
expect(Object.keys(Group.associations)).to.deep.equal(['primaryUsers', 'secondaryUsers'])
})
})
describe('setAssociation', function() {
it('can set the association with declared primary keys...', function(done) {
var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING })
......
/* jshint camelcase: false */
/* jshint camelcase: false, expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
......@@ -11,6 +11,18 @@ var chai = require('chai')
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("HasMany"), function() {
describe("Model.associations", function () {
it("should store all assocations when associting to the same table multiple times", function () {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
Group.hasMany(User, { foreignKey: 'primaryGroupId', as: 'primaryUsers' });
Group.hasMany(User, { foreignKey: 'secondaryGroupId', as: 'secondaryUsers' });
expect(Object.keys(Group.associations)).to.deep.equal(['primaryUsers', 'secondaryUsers'])
})
})
describe('(1:N)', function() {
describe('hasSingle', function() {
beforeEach(function(done) {
......
/* jshint camelcase: false */
/* jshint camelcase: false, expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
......@@ -7,6 +7,18 @@ var chai = require('chai')
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("HasOne"), function() {
describe("Model.associations", function () {
it("should store all assocations when associting to the same table multiple times", function () {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
Group.hasOne(User, { foreignKey: 'primaryGroupId', as: 'primaryUsers' });
Group.hasOne(User, { foreignKey: 'secondaryGroupId', as: 'secondaryUsers' });
expect(Object.keys(Group.associations)).to.deep.equal(['primaryUsers', 'secondaryUsers'])
})
})
describe('getAssocation', function() {
it('should be able to handle a where object that\'s a first class citizen.', function(done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
......
......@@ -1887,6 +1887,23 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
it('allows mulitple assocations of the same model with different alias', function (done) {
var self = this
this.Worker.belongsTo(this.Task, { as: 'ToDo' })
this.Worker.belongsTo(this.Task, { as: 'DoTo' })
this.init(function () {
self.Worker.find({
include: [
{ model: self.Task, as: 'ToDo' },
{ model: self.Task, as: 'DoTo' }
]
}).success(function () {
done()
})
})
})
})
describe('hasOne', function() {
......@@ -2008,6 +2025,23 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done()
})
})
it('allows mulitple assocations of the same model with different alias', function (done) {
var self = this
this.Worker.hasOne(this.Task, { as: 'DoTo' })
this.init(function () {
self.Worker.find({
include: [
{ model: self.Task, as: 'ToDo' },
{ model: self.Task, as: 'DoTo' }
]
}).success(function () {
// Just being able to include both is shows that this test works, so no assertions
done()
})
})
})
})
})
......@@ -2134,6 +2168,22 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done()
})
})
it('allows mulitple assocations of the same model with different alias', function (done) {
var self = this
this.Worker.hasMany(this.Task, { as: 'DoTos' })
this.init(function () {
self.Worker.find({
include: [
{ model: self.Task, as: 'ToDos' },
{ model: self.Task, as: 'DoTos' }
]
}).success(function () {
done()
})
})
})
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!