Texperts.findAndCountAll({
subQuery:false,
where:{
...(searchKey && {
[Op.or]: [
{ '$user.full_name$': { [Op.like]: `%${searchKey}%` } },
{ '$tags1.tag.name$': { [Op.like]: `%${searchKey}%` } },
],
}),
},
attributes: {
exclude: ["createdAt", "updatedAt", "deletedAt"],
include: [
[
Sequelize.literal(`(
SELECT count(*) FROM texpert_service_bookings AS bookings
INNER JOIN texpert_services AS services ON services.id = bookings.service_id INNER JOIN texperts as t ON t.id=services.texpert_id AND t.id=Texperts.id
)`),
"totalBookings",
],
[
Sequelize.literal(`
CASE
WHEN Texperts.user_id = ${user_id} THEN NULL
ELSE EXISTS (
SELECT 1
FROM connections
WHERE (connections.accepted = ${true}) AND (connections.from_userId = ${user_id} AND connections.to_userId = Texperts.user_id) OR (connections.to_userId = ${user_id} AND connections.from_userId = Texperts.user_id)
)
END
`),
"is_connected",
],
],
},
include: [
{
model: User,
where: {
userId:{[Op.not]:user_id}
},
attributes: USER_DEFAULT_ATTRIBUTES,
},
{
model: TexpertTags,
as:"tags1",
subQuery:false,
attributes: ["tag_id"],
where: {
...(tag_id && {tag_id }),
},
include: [
{
model: TexpertTagsData,
subQuery:false,
as:"tag",
attributes: ["id", "name"],
},
],
},
{
model: TexpertTags,
as:"tags",
attributes: ["id"],
include: [
{
model: TexpertTagsData,
attributes: ["id", "name"],
},
],
},
{
model: TexpertServices,
required:true,
attributes: {
exclude: ["createdAt", "updatedAt", "deletedAt"],
}
},
],
order: [[Sequelize.literal("is_connected"),"DESC"],[Sequelize.literal("totalBookings"), "DESC"]],
distinct:true,
// Pagination
...(page &&
size && {
offset: (page - 1) * size,
limit: size,
}),
});
In this TexpertsTags has hasMany relation with Texperts table. and TexpertTagsData table is belongs to TexpertsTags table.
when i try to get data by like query in the parent where condition with subQuery: true, error occured ( unknown column tags1->tab.name), when subQuery: false , searching is working fine but pagination is not working. data is duplicating and data is getting with unexpected counts.
{ ‘$tags1.tag.name$’: { [Op.like]: %${searchKey}%
} } when this searching is removed pagination and other search working fine.
{ ‘$tags1.tag.name$’: { [Op.like]: %${searchKey}%
} } when removing this search everything works fine.