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 6e6c8430
authored
Dec 29, 2013
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes som setter/adder bugs introduced with assumed through models
1 parent
c49d6c13
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
20 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/has-many-double-linked.js
View file @
6e6c843
...
...
@@ -129,7 +129,9 @@ module.exports = (function() {
changedAssociation
.
where
[
self
.
association
.
identifier
]
=
self
.
instance
[
self
.
association
.
identifier
]
||
self
.
instance
.
id
changedAssociation
.
where
[
foreignIdentifier
]
=
newObj
[
foreignIdentifier
]
||
newObj
.
id
changedAssociations
.
push
(
changedAssociation
)
if
(
Object
.
keys
(
changedAssociation
.
attributes
).
length
)
{
changedAssociations
.
push
(
changedAssociation
)
}
}
})
...
...
@@ -187,11 +189,15 @@ module.exports = (function() {
attributes
[
this
.
association
.
identifier
]
=
((
sourceKeys
.
length
===
1
)
?
this
.
instance
[
sourceKeys
[
0
]]
:
this
.
instance
.
id
)
attributes
[
foreignIdentifier
]
=
((
targetKeys
.
length
===
1
)
?
newAssociation
[
targetKeys
[
0
]]
:
newAssociation
.
id
)
if
(
exists
)
{
// implies hasJoinTableModel === true
if
(
exists
)
{
var
where
=
attributes
attributes
=
Utils
.
_
.
defaults
({},
newAssociation
[
a
ssociation
.
through
.
name
],
additionalAttributes
)
attributes
=
Utils
.
_
.
defaults
({},
newAssociation
[
targetA
ssociation
.
through
.
name
],
additionalAttributes
)
targetAssociation
.
through
.
update
(
attributes
,
where
).
proxy
(
emitterProxy
)
if
(
Object
.
keys
(
attributes
).
length
)
{
targetAssociation
.
through
.
update
(
attributes
,
where
).
proxy
(
emitterProxy
)
}
else
{
emitterProxy
.
emit
(
'success'
)
}
}
else
{
attributes
=
Utils
.
_
.
defaults
(
attributes
,
newAssociation
[
targetAssociation
.
through
.
name
],
additionalAttributes
)
...
...
lib/associations/has-many.js
View file @
6e6c843
...
...
@@ -241,12 +241,7 @@ module.exports = (function() {
var
Class
=
self
.
doubleLinked
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectSetter
(
emitter
,
oldAssociatedObjects
,
newAssociatedObjects
,
defaultAttributes
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
}).
run
()
}
...
...
@@ -260,9 +255,9 @@ module.exports = (function() {
where
[
newAssociatedObject
.
daoFactory
.
tableName
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]({
where
:
where
})
.
error
(
function
(
err
){
emitter
.
emit
(
'error'
,
err
)
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
success
(
function
(
currentAssociatedObjects
)
{
if
(
currentAssociatedObjects
.
length
===
0
||
self
.
hasJoinTableModel
===
true
)
{
if
(
currentAssociatedObjects
.
length
===
0
||
Object
(
self
.
through
)
===
self
.
through
)
{
var
Class
=
self
.
doubleLinked
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
newAssociatedObject
,
additionalAttributes
,
!!
currentAssociatedObjects
.
length
)
}
else
{
...
...
@@ -274,7 +269,7 @@ module.exports = (function() {
obj
[
this
.
accessors
.
remove
]
=
function
(
oldAssociatedObject
)
{
var
instance
=
this
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
(
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]().
success
(
function
(
currentAssociatedObjects
)
{
var
newAssociations
=
[]
,
oldAssociations
=
[]
...
...
@@ -302,12 +297,10 @@ module.exports = (function() {
var
run
=
function
(
err
)
{
if
(
!!
err
)
{
return
customEventE
mitter
.
emit
(
'error'
,
err
)
return
e
mitter
.
emit
(
'error'
,
err
)
}
instance
[
self
.
accessors
.
set
](
newAssociations
)
.
success
(
function
()
{
customEventEmitter
.
emit
(
'success'
,
null
)
})
.
error
(
function
(
err
)
{
customEventEmitter
.
emit
(
'error'
,
err
)
})
instance
[
self
.
accessors
.
set
](
newAssociations
).
proxy
(
emitter
)
}
if
(
oldAssociations
.
length
>
0
)
{
...
...
@@ -317,7 +310,6 @@ module.exports = (function() {
}
})
})
return
customEventEmitter
.
run
()
}
return
this
...
...
test/associations/has-many.test.js
View file @
6e6c843
...
...
@@ -766,8 +766,6 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
describe
(
'inserting in join table'
,
function
()
{
describe
(
'add'
,
function
()
{
it
(
'should insert data provided on the object into the join table'
,
function
(
done
)
{
var
self
=
this
...
...
@@ -800,6 +798,32 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
it
(
'should be able to add twice (second call result in UPDATE call) without any attributes (and timestamps off) on the through model'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{},
{
timestamps
:
false
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{},
{
timestamps
:
false
})
,
WorkerTasks
=
this
.
sequelize
.
define
(
'WorkerTasks'
,
{},
{
timestamps
:
false
})
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
Worker
.
create
().
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
Task
.
create
().
done
(
function
(
err
,
task
)
{
expect
(
err
).
not
.
to
.
be
.
ok
worker
.
addTask
(
task
).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
worker
.
addTask
(
task
).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
done
()
})
})
})
})
})
})
})
describe
(
'set'
,
function
()
{
...
...
@@ -832,6 +856,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
it
(
'should be able to set twice (second call result in UPDATE calls) without any attributes (and timestamps off) on the through model'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{},
{
timestamps
:
false
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{},
{
timestamps
:
false
})
,
WorkerTasks
=
this
.
sequelize
.
define
(
'WorkerTasks'
,
{},
{
timestamps
:
false
})
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
Worker
.
create
().
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
Task
.
bulkCreate
([{},
{}]).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
Task
.
findAll
().
done
(
function
(
err
,
tasks
)
{
expect
(
err
).
not
.
to
.
be
.
ok
worker
.
setTasks
(
tasks
).
done
(
function
(
err
)
{
worker
.
setTasks
(
tasks
).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
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