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 db597c1d
authored
Jan 02, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change some tests, break BC
1 parent
931cfaad
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
22 deletions
lib/associations/belongs-to.js
lib/dialects/abstract/query-generator.js
test/associations/has-many.test.js
test/associations/multiple-level-filters.test.js
test/dao.test.js
lib/associations/belongs-to.js
View file @
db597c1
...
@@ -13,7 +13,7 @@ module.exports = (function() {
...
@@ -13,7 +13,7 @@ module.exports = (function() {
this
.
as
=
this
.
options
.
as
this
.
as
=
this
.
options
.
as
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
as
)
{
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
as
)
{
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
as
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
}
}
if
(
!
this
.
as
)
{
if
(
!
this
.
as
)
{
...
@@ -38,7 +38,8 @@ module.exports = (function() {
...
@@ -38,7 +38,8 @@ module.exports = (function() {
,
targetKeys
=
Object
.
keys
(
this
.
target
.
primaryKeys
)
,
targetKeys
=
Object
.
keys
(
this
.
target
.
primaryKeys
)
,
keyType
=
((
this
.
target
.
hasPrimaryKeys
&&
targetKeys
.
length
===
1
)
?
this
.
target
.
rawAttributes
[
targetKeys
[
0
]].
type
:
DataTypes
.
INTEGER
)
,
keyType
=
((
this
.
target
.
hasPrimaryKeys
&&
targetKeys
.
length
===
1
)
?
this
.
target
.
rawAttributes
[
targetKeys
[
0
]].
type
:
DataTypes
.
INTEGER
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
as
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
//this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.as + "Id", this.source.options.underscored)
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyType
}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyType
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
target
,
this
.
source
,
this
.
options
)
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
target
,
this
.
source
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
...
...
lib/dialects/abstract/query-generator.js
View file @
db597c1
...
@@ -437,7 +437,7 @@ module.exports = (function() {
...
@@ -437,7 +437,7 @@ module.exports = (function() {
options
.
attributes
=
optAttributes
.
join
(
', '
)
options
.
attributes
=
optAttributes
.
join
(
', '
)
}
}
var
conditionalJoins
=
options
.
hasMultiAssociation
&&
this
.
getConditionalJoins
(
options
,
factory
),
var
conditionalJoins
=
options
.
hasMultiAssociation
&&
(
options
.
limit
||
options
.
offset
)
&&
this
.
getConditionalJoins
(
options
,
factory
),
query
;
query
;
if
(
conditionalJoins
)
{
if
(
conditionalJoins
)
{
...
@@ -613,7 +613,6 @@ module.exports = (function() {
...
@@ -613,7 +613,6 @@ module.exports = (function() {
,
attributePart
=
associationParts
.
pop
()
,
attributePart
=
associationParts
.
pop
()
,
self
=
this
,
self
=
this
return
associationParts
.
every
(
function
(
attribute
)
{
return
associationParts
.
every
(
function
(
attribute
)
{
var
association
=
self
.
findAssociation
(
attribute
,
dao
);
var
association
=
self
.
findAssociation
(
attribute
,
dao
);
if
(
!
association
)
return
false
;
if
(
!
association
)
return
false
;
...
@@ -627,13 +626,15 @@ module.exports = (function() {
...
@@ -627,13 +626,15 @@ module.exports = (function() {
,
attributePart
=
associationParts
.
pop
()
,
attributePart
=
associationParts
.
pop
()
,
self
=
this
,
self
=
this
,
association
,
association
,
keyParts
=
[]
associationParts
.
forEach
(
function
(
attribute
)
{
associationParts
.
forEach
(
function
(
attribute
)
{
association
=
self
.
findAssociation
(
attribute
,
dao
)
association
=
self
.
findAssociation
(
attribute
,
dao
)
dao
=
self
.
findAssociation
(
attribute
,
dao
).
target
;
dao
=
association
.
target
;
keyParts
.
push
(
association
.
as
||
association
.
options
.
as
||
dao
.
tableName
)
})
})
return
(
association
.
as
||
association
.
options
.
as
||
dao
.
tableName
)
+
'.'
+
attributePart
;
return
this
.
quoteIdentifier
(
keyParts
.
join
(
'.'
))
+
'.'
+
this
.
quote
(
attributePart
)
;
},
},
getConditionalJoins
:
function
(
options
,
originalDao
){
getConditionalJoins
:
function
(
options
,
originalDao
){
...
@@ -702,10 +703,12 @@ module.exports = (function() {
...
@@ -702,10 +703,12 @@ module.exports = (function() {
if
(
this
.
isAssociationFilter
(
key
,
dao
)){
if
(
this
.
isAssociationFilter
(
key
,
dao
)){
key
=
this
.
getAssociationFilterColumn
(
key
,
dao
);
key
=
this
.
getAssociationFilterColumn
(
key
,
dao
);
}
else
{
key
=
this
.
quoteIdentifiers
(
key
)
}
}
//handle qualified key names
//handle qualified key names
var
_key
=
this
.
quoteIdentifiers
(
key
)
var
_key
=
key
,
_value
=
null
,
_value
=
null
if
(
Array
.
isArray
(
value
))
{
if
(
Array
.
isArray
(
value
))
{
...
...
test/associations/has-many.test.js
View file @
db597c1
...
@@ -930,7 +930,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -930,7 +930,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
describe
(
'source belongs to target'
,
function
()
{
describe
(
'source belongs to target'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
this
.
A
.
belongsTo
(
this
.
B
,
{
as
:
'relation1'
}
)
this
.
A
.
belongsTo
(
this
.
B
)
this
.
A
.
hasMany
(
this
.
B
,
{
as
:
'relation2'
})
this
.
A
.
hasMany
(
this
.
B
,
{
as
:
'relation2'
})
this
.
B
.
hasMany
(
this
.
A
,
{
as
:
'relation2'
})
this
.
B
.
hasMany
(
this
.
A
,
{
as
:
'relation2'
})
...
@@ -948,7 +948,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -948,7 +948,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
a1
a1
.
save
()
.
save
()
.
then
(
function
()
{
return
b1
.
save
()
})
.
then
(
function
()
{
return
b1
.
save
()
})
.
then
(
function
()
{
return
a1
.
set
Relation1
(
b1
)
})
.
then
(
function
()
{
return
a1
.
set
B
(
b1
)
})
.
then
(
function
()
{
return
self
.
A
.
find
({
where
:
{
name
:
'a1'
}
})
})
.
then
(
function
()
{
return
self
.
A
.
find
({
where
:
{
name
:
'a1'
}
})
})
.
done
(
function
(
a
)
{
.
done
(
function
(
a
)
{
expect
(
a
.
bId
).
to
.
be
.
eq
(
b1
.
id
)
expect
(
a
.
bId
).
to
.
be
.
eq
(
b1
.
id
)
...
@@ -959,7 +959,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -959,7 +959,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
describe
(
'target belongs to source'
,
function
()
{
describe
(
'target belongs to source'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
this
.
B
.
belongsTo
(
this
.
A
,
{
as
:
'relation1'
}
)
this
.
B
.
belongsTo
(
this
.
A
)
this
.
A
.
hasMany
(
this
.
B
,
{
as
:
'relation2'
})
this
.
A
.
hasMany
(
this
.
B
,
{
as
:
'relation2'
})
this
.
B
.
hasMany
(
this
.
A
,
{
as
:
'relation2'
})
this
.
B
.
hasMany
(
this
.
A
,
{
as
:
'relation2'
})
...
@@ -977,7 +977,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -977,7 +977,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
a1
a1
.
save
()
.
save
()
.
then
(
function
()
{
return
b1
.
save
()
})
.
then
(
function
()
{
return
b1
.
save
()
})
.
then
(
function
()
{
return
b1
.
set
Relation1
(
a1
)
})
.
then
(
function
()
{
return
b1
.
set
A
(
a1
)
})
.
then
(
function
()
{
return
self
.
B
.
find
({
where
:
{
name
:
'b1'
}
})
})
.
then
(
function
()
{
return
self
.
B
.
find
({
where
:
{
name
:
'b1'
}
})
})
.
done
(
function
(
b
)
{
.
done
(
function
(
b
)
{
expect
(
b
.
aId
).
to
.
be
.
eq
(
a1
.
id
)
expect
(
b
.
aId
).
to
.
be
.
eq
(
a1
.
id
)
...
...
test/associations/multiple-level-filters.test.js
View file @
db597c1
...
@@ -18,7 +18,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
...
@@ -18,7 +18,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
Task
.
belongsTo
(
Project
);
Task
.
belongsTo
(
Project
);
Project
.
hasMany
(
Task
);
Project
.
hasMany
(
Task
);
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([{
User
.
bulkCreate
([{
username
:
'leia'
username
:
'leia'
...
@@ -47,8 +46,15 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
...
@@ -47,8 +46,15 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
Task
.
findAll
({
Task
.
findAll
({
where
:
{
where
:
{
'project.user.username'
:
'leia'
'project.user.username'
:
'leia'
}
},
}).
success
(
function
(
tasks
){
include
:
[
{
model
:
Project
,
include
:
[
User
]}
]
}).
done
(
function
(
err
,
tasks
){
expect
(
err
).
not
.
to
.
be
.
ok
try
{
try
{
expect
(
tasks
.
length
).
to
.
be
.
equal
(
2
);
expect
(
tasks
.
length
).
to
.
be
.
equal
(
2
);
expect
(
tasks
[
0
].
title
).
to
.
be
.
equal
(
'fight empire'
);
expect
(
tasks
[
0
].
title
).
to
.
be
.
equal
(
'fight empire'
);
...
@@ -106,7 +112,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
...
@@ -106,7 +112,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
where
:
{
where
:
{
'project.user.username'
:
'leia'
,
'project.user.username'
:
'leia'
,
'project.user.id'
:
1
'project.user.id'
:
1
}
},
include
:
[
{
model
:
Project
,
include
:
[
User
]}
]
}).
success
(
function
(
tasks
){
}).
success
(
function
(
tasks
){
try
{
try
{
expect
(
tasks
.
length
).
to
.
be
.
equal
(
2
);
expect
(
tasks
.
length
).
to
.
be
.
equal
(
2
);
...
@@ -134,7 +145,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
...
@@ -134,7 +145,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
Task
.
belongsTo
(
Project
);
Task
.
belongsTo
(
Project
);
Project
.
hasMany
(
Task
);
Project
.
hasMany
(
Task
);
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([{
User
.
bulkCreate
([{
username
:
'leia'
username
:
'leia'
...
@@ -164,8 +174,13 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
...
@@ -164,8 +174,13 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User
.
findAll
({
User
.
findAll
({
where
:
{
where
:
{
'projects.tasks.title'
:
'fight empire'
'projects.tasks.title'
:
'fight empire'
}
},
}).
success
(
function
(
users
){
include
:
[
{
model
:
Project
,
include
:
[
Task
]}
]
}).
done
(
function
(
err
,
users
){
try
{
try
{
expect
(
users
.
length
).
to
.
be
.
equal
(
1
);
expect
(
users
.
length
).
to
.
be
.
equal
(
1
);
expect
(
users
[
0
].
username
).
to
.
be
.
equal
(
'leia'
);
expect
(
users
[
0
].
username
).
to
.
be
.
equal
(
'leia'
);
...
@@ -208,7 +223,10 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
...
@@ -208,7 +223,10 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User
.
findAll
({
User
.
findAll
({
where
:
{
where
:
{
'projects.title'
:
'republic'
'projects.title'
:
'republic'
}
},
include
:
[
{
model
:
Project
}
]
}).
success
(
function
(
users
){
}).
success
(
function
(
users
){
try
{
try
{
expect
(
users
.
length
).
to
.
be
.
equal
(
1
);
expect
(
users
.
length
).
to
.
be
.
equal
(
1
);
...
...
test/dao.test.js
View file @
db597c1
...
@@ -920,8 +920,8 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
...
@@ -920,8 +920,8 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
overdue_days
:
DataTypes
.
INTEGER
overdue_days
:
DataTypes
.
INTEGER
},
{
timestamps
:
false
})
},
{
timestamps
:
false
})
this
.
UserEager
.
hasMany
(
this
.
ProjectEager
,
{
as
:
'Projects'
})
this
.
UserEager
.
hasMany
(
this
.
ProjectEager
,
{
as
:
'Projects'
,
foreignKey
:
'ProjectId'
})
this
.
ProjectEager
.
belongsTo
(
this
.
UserEager
,
{
as
:
'Poobah'
})
this
.
ProjectEager
.
belongsTo
(
this
.
UserEager
,
{
as
:
'Poobah'
,
foreignKey
:
'ProjectId'
})
self
.
UserEager
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
UserEager
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
ProjectEager
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
ProjectEager
.
sync
({
force
:
true
}).
success
(
function
()
{
...
@@ -1016,7 +1016,6 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
...
@@ -1016,7 +1016,6 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
self
.
ProjectEager
.
create
({
title
:
'party'
,
overdue_days
:
2
}).
success
(
function
(
party
)
{
self
.
ProjectEager
.
create
({
title
:
'party'
,
overdue_days
:
2
}).
success
(
function
(
party
)
{
user
.
setProjects
([
homework
,
party
]).
success
(
function
()
{
user
.
setProjects
([
homework
,
party
]).
success
(
function
()
{
self
.
ProjectEager
.
findAll
({
include
:
[{
model
:
self
.
UserEager
,
as
:
'Poobah'
}]}).
success
(
function
(
projects
)
{
self
.
ProjectEager
.
findAll
({
include
:
[{
model
:
self
.
UserEager
,
as
:
'Poobah'
}]}).
success
(
function
(
projects
)
{
expect
(
projects
.
length
).
to
.
equal
(
2
)
expect
(
projects
.
length
).
to
.
equal
(
2
)
expect
(
projects
[
0
].
poobah
).
to
.
exist
expect
(
projects
[
0
].
poobah
).
to
.
exist
expect
(
projects
[
1
].
poobah
).
to
.
exist
expect
(
projects
[
1
].
poobah
).
to
.
exist
...
...
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