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 d579cef6
authored
Jun 13, 2012
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow self association without junction table by specifying isManyMany: false in options
1 parent
a2a07c2e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
2 deletions
lib/associations/has-many.js
package.json
spec-jasmine/associations/has-many.spec.js
spec-jasmine/config/config.js
lib/associations/has-many.js
View file @
d579cef
...
...
@@ -9,6 +9,7 @@ module.exports = (function() {
this
.
source
=
srcDAO
this
.
target
=
targetDAO
this
.
options
=
options
this
.
isManyMany
=
this
.
options
.
isManyMany
===
undefined
?
true
:
this
.
options
.
isManyMany
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
this
.
associationAccessor
=
this
.
combinedName
=
this
.
options
.
joinTableName
||
Utils
.
combineTableNames
(
...
...
@@ -34,7 +35,7 @@ module.exports = (function() {
// is there already a single sided association between the source and the target?
// or is the association on the model itself?
if
(
this
.
isSelfAssociation
||
multiAssociation
)
{
if
(
(
this
.
isSelfAssociation
&&
this
.
isManyMany
)
||
multiAssociation
)
{
// remove the obsolete association identifier from the source
if
(
this
.
isSelfAssociation
)
{
this
.
foreignIdentifier
=
Utils
.
_
.
underscoredIf
((
this
.
options
.
as
||
this
.
target
.
tableName
)
+
'Id'
,
this
.
options
.
underscored
)
...
...
package.json
View file @
d579cef
...
...
@@ -19,7 +19,7 @@
"generic-pool"
:
"1.0.9"
},
"devDependencies"
:
{
"jasmine-node"
:
"1.0.
22
"
,
"jasmine-node"
:
"1.0.
17
"
,
"sqlite3"
:
">=2.0.0"
,
"pg"
:
"0.6.x"
,
"buster"
:
"0.5.1"
...
...
spec-jasmine/associations/has-many.spec.js
View file @
d579cef
...
...
@@ -93,6 +93,67 @@ describe('HasMany', function() {
})
})
it
(
"should allow selfAssociation to be single linked (only one DAO is created)"
,
function
()
{
var
oldLength
=
sequelize
.
daoFactoryManager
.
daos
.
length
;
var
Comment
=
sequelize
.
define
(
'Comment'
,
{
content
:
Sequelize
.
STRING
})
Comment
.
belongsTo
(
Comment
,
{
as
:
"Parent"
});
Comment
.
hasMany
(
Comment
,
{
as
:
'Children'
,
foreignKey
:
"ParentId"
,
isManyMany
:
false
})
expect
(
sequelize
.
daoFactoryManager
.
daos
.
length
).
toEqual
(
oldLength
+
1
)
Helpers
.
async
(
function
(
done
)
{
Comment
.
sync
({
force
:
true
}).
success
(
function
()
{
done
()
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
create
({
content
:
'parentComment'
}).
success
(
function
(
p
)
{
parent
=
p
done
()
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
create
({
content
:
'child1'
}).
success
(
function
(
child1
)
{
Comment
.
find
({
where
:
{
content
:
'parentComment'
}}).
success
(
function
(
parent
)
{
child1
.
setParent
(
parent
).
success
(
function
()
{
done
()
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
create
({
content
:
'child2'
}).
success
(
function
(
child2
)
{
child2
.
setParent
(
parent
).
success
(
function
()
{
done
()
})
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
find
({
where
:
{
content
:
'parentComment'
}}).
success
(
function
(
parent
)
{
parent
.
getChildren
().
success
(
function
(
children
)
{
expect
(
children
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
it
(
"should still use many to many for selfAssociation by default (two DAOs are created)"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
oldLength
=
sequelize
.
daoFactoryManager
.
daos
.
length
;
var
Comment
=
sequelize
.
define
(
'Comment'
,
{
content
:
Sequelize
.
STRING
})
Comment
.
belongsTo
(
Comment
,
{
as
:
"Parent"
})
Comment
.
hasMany
(
Comment
,
{
as
:
'Children'
})
expect
(
sequelize
.
daoFactoryManager
.
daos
.
length
).
toEqual
(
oldLength
+
2
)
done
();
})
})
})
describe
(
'bi-directional'
,
function
()
{
...
...
spec-jasmine/config/config.js
View file @
d579cef
...
...
@@ -19,6 +19,7 @@ module.exports = {
postgres
:
{
database
:
'sequelize_test'
,
username
:
"postgres"
,
password
:
'abc'
,
port
:
5432
}
}
...
...
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