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 4aca8bdd
authored
Oct 03, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed foreign key deletion for mixed associations
1 parent
94ec0379
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
0 deletions
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/has-many.js
View file @
4aca8bd
...
@@ -48,6 +48,7 @@ module.exports = (function() {
...
@@ -48,6 +48,7 @@ module.exports = (function() {
HasMany
.
prototype
.
injectAttributes
=
function
()
{
HasMany
.
prototype
.
injectAttributes
=
function
()
{
var
multiAssociation
=
this
.
target
.
associations
.
hasOwnProperty
(
this
.
associationAccessor
)
var
multiAssociation
=
this
.
target
.
associations
.
hasOwnProperty
(
this
.
associationAccessor
)
,
self
=
this
,
self
=
this
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
options
.
underscored
)
// is there already a single sided association between the source and the target?
// is there already a single sided association between the source and the target?
...
@@ -59,9 +60,15 @@ module.exports = (function() {
...
@@ -59,9 +60,15 @@ module.exports = (function() {
}
else
{
}
else
{
this
.
foreignIdentifier
=
this
.
target
.
associations
[
this
.
associationAccessor
].
identifier
this
.
foreignIdentifier
=
this
.
target
.
associations
[
this
.
associationAccessor
].
identifier
this
.
target
.
associations
[
this
.
associationAccessor
].
foreignIdentifier
=
this
.
identifier
this
.
target
.
associations
[
this
.
associationAccessor
].
foreignIdentifier
=
this
.
identifier
if
(
isForeignKeyDeletionAllowedFor
.
call
(
this
,
this
.
source
,
this
.
foreignIdentifier
))
{
delete
this
.
source
.
rawAttributes
[
this
.
foreignIdentifier
]
delete
this
.
source
.
rawAttributes
[
this
.
foreignIdentifier
]
}
if
(
isForeignKeyDeletionAllowedFor
.
call
(
this
,
this
.
target
,
this
.
identifier
))
{
delete
this
.
target
.
associations
[
this
.
associationAccessor
].
source
.
rawAttributes
[
this
.
identifier
]
delete
this
.
target
.
associations
[
this
.
associationAccessor
].
source
.
rawAttributes
[
this
.
identifier
]
}
}
}
// define a new model, which connects the models
// define a new model, which connects the models
var
combinedTableAttributes
=
{}
var
combinedTableAttributes
=
{}
...
@@ -256,5 +263,27 @@ module.exports = (function() {
...
@@ -256,5 +263,27 @@ module.exports = (function() {
return
this
return
this
}
}
/**
* The method checks if it is ok to delete the previously defined foreign key.
* This is done because we need to keep the foreign key if another association
* is depending on it.
*
* @param {DaoFactory} daoFactory The source or target DaoFactory of this assocation
* @param {[type]} identifier The name of the foreign key identifier
* @return {Boolean} Whether or not the deletion of the foreign key is ok.
*/
var
isForeignKeyDeletionAllowedFor
=
function
(
daoFactory
,
identifier
)
{
var
isAllowed
=
true
,
associationNames
=
Utils
.
_
.
without
(
Object
.
keys
(
daoFactory
.
associations
),
this
.
associationAccessor
)
associationNames
.
forEach
(
function
(
associationName
)
{
if
(
daoFactory
.
associations
[
associationName
].
identifier
===
identifier
)
{
isAllowed
=
false
}
})
return
isAllowed
}
return
HasMany
return
HasMany
})()
})()
test/associations/has-many.test.js
View file @
4aca8bd
...
@@ -710,6 +710,71 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -710,6 +710,71 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
})
})
describe
(
'belongsTo and hasMany at once'
,
function
()
{
beforeEach
(
function
()
{
this
.
A
=
this
.
sequelize
.
define
(
'a'
,
{
name
:
Sequelize
.
STRING
})
this
.
B
=
this
.
sequelize
.
define
(
'b'
,
{
name
:
Sequelize
.
STRING
})
})
describe
(
'source belongs to target'
,
function
()
{
beforeEach
(
function
(
done
)
{
this
.
A
.
belongsTo
(
this
.
B
,
{
as
:
'relation1'
})
this
.
A
.
hasMany
(
this
.
B
,
{
as
:
'relation2'
})
this
.
B
.
hasMany
(
this
.
A
,
{
as
:
'relation2'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
done
()
})
})
it
(
'correctly uses bId in A'
,
function
(
done
)
{
var
self
=
this
var
a1
=
this
.
A
.
build
({
name
:
'a1'
})
,
b1
=
this
.
B
.
build
({
name
:
'b1'
})
a1
.
save
()
.
then
(
function
()
{
return
b1
.
save
()
})
.
then
(
function
()
{
return
a1
.
setRelation1
(
b1
)
})
.
then
(
function
()
{
return
self
.
A
.
find
({
where
:
{
name
:
'a1'
}
})
})
.
done
(
function
(
a
)
{
expect
(
a
.
bId
).
to
.
be
.
eq
(
b1
.
id
)
done
()
})
})
})
describe
(
'target belongs to source'
,
function
()
{
beforeEach
(
function
(
done
)
{
this
.
B
.
belongsTo
(
this
.
A
,
{
as
:
'relation1'
})
this
.
A
.
hasMany
(
this
.
B
,
{
as
:
'relation2'
})
this
.
B
.
hasMany
(
this
.
A
,
{
as
:
'relation2'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
done
()
})
})
it
(
'correctly uses bId in A'
,
function
(
done
)
{
var
self
=
this
var
a1
=
this
.
A
.
build
({
name
:
'a1'
})
,
b1
=
this
.
B
.
build
({
name
:
'b1'
})
a1
.
save
()
.
then
(
function
()
{
return
b1
.
save
()
})
.
then
(
function
()
{
return
b1
.
setRelation1
(
a1
)
})
.
then
(
function
()
{
return
self
.
B
.
find
({
where
:
{
name
:
'b1'
}
})
})
.
done
(
function
(
b
)
{
expect
(
b
.
aId
).
to
.
be
.
eq
(
a1
.
id
)
done
()
})
})
})
})
})
})
describe
(
"Foreign key constraints"
,
function
()
{
describe
(
"Foreign key constraints"
,
function
()
{
...
...
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