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 ea8dba06
authored
Oct 15, 2013
by
Rafael Martins
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Filter through relations prevents duplicate table in query
1 parent
78032912
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
1 deletions
lib/dialects/abstract/query-generator.js
test/associations/multiple-level-filters.test.js
lib/dialects/abstract/query-generator.js
View file @
ea8dba0
...
...
@@ -569,24 +569,30 @@ module.exports = (function() {
return
dao
.
tableName
+
'.'
+
attributePart
;
},
getConditionalJoins
:
function
(
options
,
d
ao
){
getConditionalJoins
:
function
(
options
,
originalD
ao
){
var
joins
=
''
,
self
=
this
,
joinedTables
=
{}
if
(
Utils
.
isHash
(
options
.
where
))
{
Object
.
keys
(
options
.
where
).
forEach
(
function
(
filterStr
){
var
associationParts
=
filterStr
.
split
(
'.'
)
,
attributePart
=
associationParts
.
pop
()
,
dao
=
originalDao
if
(
self
.
isAssociationFilter
(
filterStr
,
dao
))
{
associationParts
.
forEach
(
function
(
attribute
)
{
var
association
=
self
.
findAssociation
(
attribute
,
dao
);
if
(
!
joinedTables
[
association
.
target
.
tableName
]){
joinedTables
[
association
.
target
.
tableName
]
=
true
;
if
(
association
.
associationType
===
'BelongsTo'
){
joins
+=
' LEFT JOIN '
+
self
.
quoteIdentifiers
(
association
.
target
.
tableName
)
joins
+=
' ON '
+
self
.
quoteIdentifiers
(
association
.
source
.
tableName
+
'.'
+
association
.
identifier
)
joins
+=
' = '
+
self
.
quoteIdentifiers
(
association
.
target
.
tableName
+
'.'
+
association
.
target
.
autoIncrementField
)
}
else
if
(
association
.
connectorDAO
){
joinedTables
[
association
.
connectorDAO
.
tableName
]
=
true
;
joins
+=
' LEFT JOIN '
+
self
.
quoteIdentifiers
(
association
.
connectorDAO
.
tableName
)
joins
+=
' ON '
+
self
.
quoteIdentifiers
(
association
.
source
.
tableName
+
'.'
+
association
.
source
.
autoIncrementField
)
joins
+=
' = '
+
self
.
quoteIdentifiers
(
association
.
connectorDAO
.
tableName
+
'.'
+
association
.
identifier
)
...
...
@@ -599,6 +605,7 @@ module.exports = (function() {
joins
+=
' ON '
+
self
.
quoteIdentifiers
(
association
.
source
.
tableName
+
'.'
+
association
.
source
.
autoIncrementField
)
joins
+=
' = '
+
self
.
quoteIdentifiers
(
association
.
target
.
tableName
+
'.'
+
association
.
identifier
)
}
}
dao
=
association
.
target
;
});
}
...
...
test/associations/multiple-level-filters.test.js
View file @
ea8dba0
...
...
@@ -73,6 +73,73 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
})
})
it
(
'avoids duplicated tables in query'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
,
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
title
:
DataTypes
.
STRING
})
Project
.
belongsTo
(
User
);
User
.
hasMany
(
Project
)
Task
.
belongsTo
(
Project
);
Project
.
hasMany
(
Task
);
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([{
id
:
101
,
username
:
'leia'
},
{
id
:
102
,
username
:
'vader'
}]).
success
(
function
()
{
Project
.
bulkCreate
([{
id
:
201
,
UserId
:
101
,
title
:
'republic'
},{
id
:
202
,
UserId
:
102
,
title
:
'empire'
}]).
success
(
function
()
{
Task
.
bulkCreate
([{
id
:
301
,
ProjectId
:
201
,
title
:
'fight empire'
},{
id
:
302
,
ProjectId
:
201
,
title
:
'stablish republic'
},{
id
:
303
,
ProjectId
:
202
,
title
:
'destroy rebel alliance'
},{
id
:
304
,
ProjectId
:
202
,
title
:
'rule everything'
}]).
success
(
function
()
{
Task
.
findAll
({
where
:
{
'project.user.username'
:
'leia'
,
'project.user.id'
:
101
}
}).
success
(
function
(
tasks
){
try
{
expect
(
tasks
.
length
).
to
.
be
.
equal
(
2
);
expect
(
tasks
[
0
].
title
).
to
.
be
.
equal
(
'fight empire'
);
expect
(
tasks
[
1
].
title
).
to
.
be
.
equal
(
'stablish republic'
);
done
();
}
catch
(
e
){
done
(
e
);
}
})
});
});
});
})
})
it
(
'can filter through hasMany'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
...
...
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