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 51821a8b
authored
Sep 01, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
insert into join table
1 parent
18243c9f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
3 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 @
51821a8
...
...
@@ -10,7 +10,9 @@ module.exports = (function() {
var
self
=
this
,
_options
=
options
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
where
=
{},
options
=
_options
||
{}
var
where
=
{}
,
options
=
_options
||
{}
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
//fully qualify
var
instancePrimaryKeys
=
Object
.
keys
(
self
.
instance
.
daoFactory
.
primaryKeys
)
...
...
@@ -25,6 +27,21 @@ module.exports = (function() {
foreignPrimary
=
foreignPrimary
.
length
===
1
?
foreignPrimary
[
0
]
:
'id'
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
foreignKey
]
=
{
join
:
self
.
__factory
.
target
.
tableName
+
"."
+
foreignPrimary
}
if
(
association
.
customJoinTableModel
)
{
options
.
attributes
=
[]
Utils
.
_
.
forOwn
(
self
.
__factory
.
connectorDAO
.
rawAttributes
,
function
(
elem
,
key
)
{
if
(
!
(
key
in
self
.
__factory
.
connectorDAO
.
primaryKeys
))
{
options
.
attributes
.
push
(
self
.
__factory
.
target
.
QueryInterface
.
quoteIdentifier
(
self
.
__factory
.
connectorDAO
.
tableName
)
+
'.'
+
key
)
}
})
options
.
attributes
.
push
(
self
.
__factory
.
target
.
QueryInterface
.
quoteIdentifier
(
self
.
__factory
.
target
.
tableName
)
+
".*"
)
console
.
log
(
options
.
attributes
);
}
if
(
options
.
where
)
{
if
(
Array
.
isArray
(
options
.
where
))
{
smart
=
Utils
.
smartWhere
([
where
,
options
.
where
],
self
.
__factory
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
...
...
@@ -75,7 +92,7 @@ module.exports = (function() {
})
if
(
!
newObj
)
{
obsoleteAssociations
.
push
(
o
bj
)
obsoleteAssociations
.
push
(
o
ld
)
}
else
if
(
association
.
customJoinTableModel
)
{
var
changedAssociation
=
{
where
:
{},
...
...
lib/associations/has-many.js
View file @
51821a8
...
...
@@ -21,7 +21,7 @@ module.exports = (function() {
}
else
if
(
this
.
options
.
joinTableName
)
{
combinedTableName
=
this
.
options
.
joinTableName
}
else
{
Utils
.
combineTableNames
(
combinedTableName
=
Utils
.
combineTableNames
(
this
.
source
.
tableName
,
this
.
isSelfAssociation
?
(
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
)
...
...
test/associations/has-many.test.js
View file @
51821a8
...
...
@@ -567,6 +567,89 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done
()
})
})
describe
(
'join table model'
,
function
()
{
describe
(
'inserting in join table'
,
function
()
{
beforeEach
(
function
(
done
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{})
this
.
Project
=
this
.
sequelize
.
define
(
'Project'
,
{})
this
.
UserProjects
=
this
.
sequelize
.
define
(
'UserProjects'
,
{
status
:
DataTypes
.
STRING
})
this
.
User
.
hasMany
(
this
.
Project
,
{
joinTableModel
:
this
.
UserProjects
})
this
.
Project
.
hasMany
(
this
.
User
,
{
joinTableModel
:
this
.
UserProjects
})
this
.
sequelize
.
sync
().
success
(
function
()
{
done
()
})
})
describe
(
'add'
,
function
()
{
it
(
'should insert data provided on the object into the join table'
,
function
(
done
)
{
var
self
=
this
self
.
User
.
create
().
success
(
function
(
u
)
{
self
.
Project
.
create
().
success
(
function
(
p
)
{
p
.
UserProjects
=
{
status
:
'active'
}
u
.
addProject
(
p
).
success
(
function
()
{
self
.
UserProjects
.
find
({
where
:
{
UserId
:
u
.
id
,
ProjectId
:
p
.
id
}}).
success
(
function
(
up
)
{
expect
(
up
.
status
).
to
.
equal
(
'active'
)
done
()
})
})
})
})
})
it
(
'should insert data provided as a second argument into the join table'
,
function
(
done
)
{
var
self
=
this
self
.
User
.
create
().
success
(
function
(
u
)
{
self
.
Project
.
create
().
success
(
function
(
p
)
{
u
.
addProject
(
p
,
{
status
:
'active'
}).
success
(
function
()
{
self
.
UserProjects
.
find
({
where
:
{
UserId
:
u
.
id
,
ProjectId
:
p
.
id
}}).
success
(
function
(
up
)
{
expect
(
up
.
status
).
to
.
equal
(
'active'
)
done
()
})
})
})
})
})
})
describe
(
'set'
,
function
()
{
it
(
'should be able to combine properties on the associated objects, and default values'
,
function
(
done
)
{
var
self
=
this
,
_done
=
_
.
after
(
2
,
done
)
self
.
User
.
create
().
success
(
function
(
u
)
{
self
.
Project
.
bulkCreate
([{},
{}]).
success
(
function
()
{
self
.
Project
.
findAll
().
success
(
function
(
projects
)
{
var
p1
=
projects
[
0
]
,
p2
=
projects
[
1
]
p1
.
UserProjects
=
{
status
:
'inactive'
}
u
.
setProjects
([
p1
,
p2
],
{
status
:
'active'
}).
success
(
function
()
{
self
.
UserProjects
.
find
({
where
:
{
UserId
:
u
.
id
,
ProjectId
:
p1
.
id
}}).
success
(
function
(
up
)
{
expect
(
up
.
status
).
to
.
equal
(
'inactive'
)
_done
()
})
self
.
UserProjects
.
find
({
where
:
{
UserId
:
u
.
id
,
ProjectId
:
p2
.
id
}}).
success
(
function
(
up
)
{
expect
(
up
.
status
).
to
.
equal
(
'active'
)
_done
()
})
})
})
})
})
})
})
})
})
})
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