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 ae9abbbe
authored
Dec 03, 2017
by
Gabe Gorelick
Committed by
Sushant
Dec 03, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(model): ordering by renamed sub-query attribute (#8740)
1 parent
b64e01ce
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
129 additions
and
0 deletions
lib/dialects/abstract/query-generator.js
test/integration/model/findAll.test.js
lib/dialects/abstract/query-generator.js
View file @
ae9abbb
...
@@ -1688,6 +1688,16 @@ const QueryGenerator = {
...
@@ -1688,6 +1688,16 @@ const QueryGenerator = {
)
{
)
{
subQueryOrder
.
push
(
this
.
quote
(
order
,
model
,
'->'
));
subQueryOrder
.
push
(
this
.
quote
(
order
,
model
,
'->'
));
}
}
if
(
subQuery
)
{
// Handle case where sub-query renames attribute we want to order by,
// see https://github.com/sequelize/sequelize/issues/8739
const
subQueryAttribute
=
options
.
attributes
.
find
(
a
=>
Array
.
isArray
(
a
)
&&
a
[
0
]
===
order
[
0
]
&&
a
[
1
]);
if
(
subQueryAttribute
)
{
order
[
0
]
=
new
Utils
.
Col
(
subQueryAttribute
[
1
]);
}
}
mainQueryOrder
.
push
(
this
.
quote
(
order
,
model
,
'->'
));
mainQueryOrder
.
push
(
this
.
quote
(
order
,
model
,
'->'
));
}
}
}
else
if
(
options
.
order
instanceof
Utils
.
SequelizeMethod
)
{
}
else
if
(
options
.
order
instanceof
Utils
.
SequelizeMethod
)
{
...
...
test/integration/model/findAll.test.js
View file @
ae9abbb
...
@@ -665,6 +665,125 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -665,6 +665,125 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect
(
workers
[
0
].
tasks
[
0
].
title
).
to
.
equal
(
'homework'
);
expect
(
workers
[
0
].
tasks
[
0
].
title
).
to
.
equal
(
'homework'
);
});
});
});
});
// https://github.com/sequelize/sequelize/issues/8739
it
(
'supports sorting on renamed sub-query attribute'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
field
:
'some_other_name'
}
});
const
Project
=
this
.
sequelize
.
define
(
'project'
,
{
title
:
Sequelize
.
STRING
});
User
.
hasMany
(
Project
);
return
User
.
sync
({
force
:
true
})
.
then
(()
=>
Project
.
sync
({
force
:
true
}))
.
then
(()
=>
{
return
User
.
bulkCreate
([
{
name
:
'a'
},
{
name
:
'b'
},
{
name
:
'c'
}
]);
})
.
then
(()
=>
{
return
User
.
findAll
({
order
:
[
'name'
],
limit
:
2
,
// to force use of a sub-query
include
:
[
Project
]
});
})
.
then
(
users
=>
{
expect
(
users
).
to
.
have
.
lengthOf
(
2
);
expect
(
users
[
0
].
name
).
to
.
equal
(
'a'
);
expect
(
users
[
1
].
name
).
to
.
equal
(
'b'
);
});
});
it
(
'supports sorting DESC on renamed sub-query attribute'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
field
:
'some_other_name'
}
});
const
Project
=
this
.
sequelize
.
define
(
'project'
,
{
title
:
Sequelize
.
STRING
});
User
.
hasMany
(
Project
);
return
User
.
sync
({
force
:
true
})
.
then
(()
=>
Project
.
sync
({
force
:
true
}))
.
then
(()
=>
{
return
User
.
bulkCreate
([
{
name
:
'a'
},
{
name
:
'b'
},
{
name
:
'c'
}
]);
})
.
then
(()
=>
{
return
User
.
findAll
({
order
:
[[
'name'
,
'DESC'
]],
limit
:
2
,
include
:
[
Project
]
});
})
.
then
(
users
=>
{
expect
(
users
).
to
.
have
.
lengthOf
(
2
);
expect
(
users
[
0
].
name
).
to
.
equal
(
'c'
);
expect
(
users
[
1
].
name
).
to
.
equal
(
'b'
);
});
});
it
(
'supports sorting on multiple renamed sub-query attributes'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
field
:
'some_other_name'
},
age
:
{
type
:
Sequelize
.
INTEGER
,
field
:
'a_g_e'
}
});
const
Project
=
this
.
sequelize
.
define
(
'project'
,
{
title
:
Sequelize
.
STRING
});
User
.
hasMany
(
Project
);
return
User
.
sync
({
force
:
true
})
.
then
(()
=>
Project
.
sync
({
force
:
true
}))
.
then
(()
=>
{
return
User
.
bulkCreate
([
{
name
:
'a'
,
age
:
1
},
{
name
:
'a'
,
age
:
2
},
{
name
:
'b'
,
age
:
3
}
]);
})
.
then
(()
=>
{
return
User
.
findAll
({
order
:
[[
'name'
,
'ASC'
],
[
'age'
,
'DESC'
]],
limit
:
2
,
include
:
[
Project
]
});
})
.
then
(
users
=>
{
expect
(
users
).
to
.
have
.
lengthOf
(
2
);
expect
(
users
[
0
].
name
).
to
.
equal
(
'a'
);
expect
(
users
[
0
].
age
).
to
.
equal
(
2
);
expect
(
users
[
1
].
name
).
to
.
equal
(
'a'
);
expect
(
users
[
1
].
age
).
to
.
equal
(
1
);
})
.
then
(()
=>
{
return
User
.
findAll
({
order
:
[[
'name'
,
'DESC'
],
'age'
],
limit
:
2
,
include
:
[
Project
]
});
})
.
then
(
users
=>
{
expect
(
users
).
to
.
have
.
lengthOf
(
2
);
expect
(
users
[
0
].
name
).
to
.
equal
(
'b'
);
expect
(
users
[
1
].
name
).
to
.
equal
(
'a'
);
expect
(
users
[
1
].
age
).
to
.
equal
(
1
);
});
});
});
});
describe
(
'hasMany with alias'
,
()
=>
{
describe
(
'hasMany with alias'
,
()
=>
{
...
...
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