Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit a9bc35d2
authored
Sep 17, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow multiple hasmany associations of the same model
1 parent
21fbae6d
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
91 additions
and
5 deletions
lib/associations/has-many.js
test/associations/belongs-to.test.js
test/associations/has-many.test.js
test/associations/has-one.test.js
test/dao-factory.test.js
lib/associations/has-many.js
View file @
a9bc35d
...
...
@@ -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
))
...
...
test/associations/belongs-to.test.js
View file @
a9bc35d
/* 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
})
...
...
test/associations/has-many.test.js
View file @
a9bc35d
/* 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
)
{
...
...
test/associations/has-one.test.js
View file @
a9bc35d
/* 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
})
...
...
test/dao-factory.test.js
View file @
a9bc35d
...
...
@@ -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
()
})
})
})
})
})
})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment