Repository navigation
Open
Open
Copy link
Labels
Great SSCCEThis issue has a great SSCCE/MCVE/reprex posted and therefore deserves extra attention! :)status: understoodFor issues. Applied when the issue is understood / reproducible.type: bugDEPRECATED: replace with the "bug" issue type
Description
What are you doing?
const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('postgres://postgres@localhost:5432/testdb', { logging: false });
const User = sequelize.define('User', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: sequelize.fn('gen_random_uuid')
},
login: {
type: DataTypes.STRING(255),
allowNull: false,
unique: false // just for demo
}
}, {
timestamps: true,
paranoid: false,
tableName: 'users',
freezeTableName: true,
underscored: true,
modelName: 'Users',
scopes: {
page(index = 1, limit = 10) {
let $return = {
offset: 0,
limit: 10
};
if (limit !== Infinity && !isNaN(index) && !isNaN(limit)) {
$return = {
offset: (index - 1) * limit,
limit
};
}
return $return;
}
}
});
const ParentChild = sequelize.define('ParentChild', {}, {
timestamps: false,
paranoid: false,
tableName: 'users_parent_child',
freezeTableName: true,
underscored: true,
modelName: 'ParentChild'
});
User.belongsToMany(User, {
hooks: false,
as: {
singular: 'Parent',
plural: 'Parents'
},
through: {
model: ParentChild,
unique: true
},
foreignKey: 'child_id',
otherKey: 'parent_id'
});
User.belongsToMany(User, {
hooks: false,
as: {
singular: 'Child',
plural: 'Children'
},
through: {
model: ParentChild,
unique: true
},
foreignKey: 'parent_id',
otherKey: 'child_id'
});
let someParent = '';
sequelize.query(`CREATE EXTENSION IF NOT EXISTS "pgcrypto"`)
.then(() => sequelize.sync({ force: true }))
.then(() => Promise.all([
User.bulkCreate([...Array(10).keys()].map(() => ({login: 'c'}))),
User.bulkCreate([...Array(10).keys()].map(() => ({login: 'p'})))
])) // generate parents and children
.then(() => {
return Promise.all([
User.findAll({where: {login: 'c'}}),
User.findAll({where: {login: 'p'}})
]);
}) // find them
.then(([children, parents]) => {
someParent = parents[0].id;
return Promise.all(children.map(
(child, i) => child.setParents([
parents[i],
parents[(i + 1) % 10]
])
));
}) // set every child two parents
.then(() => {
return Promise.all([
User.scope({method: ['page', 1, 5]})
.findAndCountAll({
where: {
login: 'c'
},
include: [
{
association: 'Parents',
required: true,
attributes: [],
where: {
id: someParent
}
}
]
}),
User.findAndCountAll({
where: {
login: 'c'
},
include: [
{
association: 'Parents',
required: true,
attributes: [],
where: {
id: someParent
}
}
]
})
]);
}) // try to find children who has parent with id "someParent" and count them
.then(res => console.log(JSON.stringify(res, null, 4)))
.finally(() => sequelize.close());To Reproduce
Steps to reproduce the behavior:
- Run the following
What do you expect to happen?
[ { count: 2, rows: [ [User], [User] ] },
{ count: 2, rows: [ [User], [User] ] } ]What is actually happening?
[ { count: 2, rows: [] },
{ count: 2, rows: [ [User], [User] ] } ]Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any
Dialect library (pg) version: 7.12.1
Database version: Postgres 10
Sequelize version: 5.13.1
Node Version: 10.16.0
OS: Win7x64
Tested with latest release: - No
- Yes
Reacted by Michael Yates
Metadata
Metadata
Assignees
Labels
Great SSCCEThis issue has a great SSCCE/MCVE/reprex posted and therefore deserves extra attention! :)status: understoodFor issues. Applied when the issue is understood / reproducible.type: bugDEPRECATED: replace with the "bug" issue type
Type
Projects
- StatusShow more project fieldsNo status
Activity
papb commentedon Aug 13, 2019
Thanks for the great SSCCE 👍! I was able to reproduce.
Note: I made very minor edits to your SSCCE to make it even better (but it was already great!)
papb commentedon Aug 13, 2019
Do you think this could happen with another dialect other than postgres? Of course
'gen_random_uuidwould not be used. Can you try to come up with another SSCCE for other dialects?MegaFAX commentedon Aug 13, 2019
Thx, I tried to do this SSCCE. I think this dont depend on dialect, because this bug with self multi association. It generate SQL that has multiple "AS" and db dont know which one need to use. SQL, that genered with "wrong" query:
but when I change some
it's right, or, as I think, it must be something like
mjy78 commentedon Sep 22, 2019
I'm seeing a similar symptom where count is correct, but rows are empty, when using limit/offset (also Postgres dialect). In my case I've enabled query logging, and can actually see the rows are being returned from the query in both cases (with and without limit/offset).
I've dug deeper and it looks like the call to AbstractQuery._groupJoinData() inside of handleSelectQuery() is wiping out the result set after the data is returned.
Looking closer at the data that is being fed into this function, when limit/offset is in use, it looks like the primary key of the parent model is included in each object twice, one with a key of "_id" (the field name as defined in the model definition) and then also as "installation_request_id" (which is the db column name for the pk field).
I'll keep digging further into this function, but just mentioning it here in case it is of any consequence.
MegaFAX commentedon Sep 22, 2019
Any forecast?
papb commentedon Oct 4, 2019
Hi @mjy78 thanks for the post, hopefully you can dig further indeed!
@MegaFAX I'm sorry but I am super busy already, I can't help right now...
hoan006 commentedon Dec 5, 2019
I suppose the combination of
limitandbelongsToMany(in my situationhasMany) relationship confused the _groupJoinData() as @mjy78 observed. I have to do it the Rails/Django way to workaround this issue:hasManyinclude associations. Only returnidattributes.ids above, removinglimit,offsetparameters.theRichu commentedon Jan 28, 2020
I'm struggle with this problem, any progress?
9 remaining items
github-actions commentedon Nov 8, 2021
testn commentedon Dec 13, 2021
oliveiragit commentedon Dec 14, 2021
BabichTatsiana commentedon Sep 22, 2022
Lynn93 commentedon Nov 8, 2022
kieded commentedon Nov 22, 2022
in what version of sequelize was this issue fixed??
ephys commentedon Nov 22, 2022
It's not, the issue is still open
moisfelix commentedon Mar 31, 2023
Nobody founded a good workaround for this issue?
RamadanIbrahem98 commentedon Nov 16, 2023
I tried a workaround that actually worked for me. I don't think if it's good for everybody.
And that's by adding
subQuery: falsewangxiaodong commentedon Jan 25, 2024
i meet the same situation
using duplicating = false can resolve for me, include[].duplicating,
inspired by @RamadanIbrahem98
miftakhussalam commentedon Sep 20, 2024
Hi, I am facing same issue, and I try to do this
change version sequelize to 6.6.2 and ORM to 6.37.2
make sure you are uninstall existing sequelize
then install again with specific version
npm install -g [email protected]npm install [email protected]That worked for me.