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 b669ea33
authored
May 09, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed self association with belongs-to
1 parent
dabdcbe5
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
1 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/associations/mixin.js
test/Model/belongsTo.js → test/Model/belongs-to.js
test/Model/has-one.js
lib/sequelize/associations/belongs-to.js
View file @
b669ea3
...
@@ -5,6 +5,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
...
@@ -5,6 +5,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this
.
source
=
srcModel
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
options
=
options
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
this
.
associationAccessor
=
this
.
isSelfAssociation
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
}
}
// the id is in the source table
// the id is in the source table
...
...
lib/sequelize/associations/mixin.js
View file @
b669ea3
...
@@ -15,7 +15,7 @@ var Associations = module.exports = {
...
@@ -15,7 +15,7 @@ var Associations = module.exports = {
// the id is in this table
// the id is in this table
var
BelongsTo
=
require
(
"./belongs-to"
)
var
BelongsTo
=
require
(
"./belongs-to"
)
var
association
=
new
BelongsTo
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
var
association
=
new
BelongsTo
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
associat
edModel
.
tableName
]
=
association
.
injectAttributes
()
this
.
associations
[
associat
ion
.
associationAccessor
]
=
association
.
injectAttributes
()
},
},
hasMany
:
function
(
associatedModel
,
options
)
{
hasMany
:
function
(
associatedModel
,
options
)
{
// the id is in the foreign table or in a connecting table
// the id is in the foreign table or in a connecting table
...
...
test/Model/belongs
T
o.js
→
test/Model/belongs
-t
o.js
View file @
b669ea3
...
@@ -104,5 +104,18 @@ module.exports = {
...
@@ -104,5 +104,18 @@ module.exports = {
})
})
})
})
})
})
},
'it should correctly handle self associations'
:
function
(
exit
)
{
var
Person
=
sequelize
.
define
(
'Person'
+
config
.
rand
(),
{
name
:
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
belongsTo
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
p
=
Person
.
build
()
assert
.
isDefined
(
p
.
setFather
)
assert
.
isDefined
(
p
.
setMother
)
exit
(
function
(){})
})
}
}
}
}
\ No newline at end of file
test/Model/has-one.js
View file @
b669ea3
...
@@ -101,6 +101,15 @@ module.exports = {
...
@@ -101,6 +101,15 @@ module.exports = {
},
},
'it should correctly associate with itself'
:
function
(
exit
)
{
'it should correctly associate with itself'
:
function
(
exit
)
{
var
Person
=
sequelize
.
define
(
'Person'
+
config
.
rand
(),
{
name
:
Sequelize
.
STRING
})
var
Person
=
sequelize
.
define
(
'Person'
+
config
.
rand
(),
{
name
:
Sequelize
.
STRING
})
Person
.
hasOne
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
hasOne
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
p
=
Person
.
build
()
assert
.
isDefined
(
p
.
setFather
)
assert
.
isDefined
(
p
.
setMother
)
exit
(
function
(){})
exit
(
function
(){})
})
}
}
}
}
\ No newline at end of file
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