Error when querying with sequelize(where.or, limit)

30 Views Asked by At

I need help. There was a problem in a query with pagination.

When I make a request like this:

    async findAllForSelectionList(dto: FindAllClientsDto) {
        const where: any = {};
        if (dto.search) {
            where[Op.or] = [
                { info: { [Op.like]: `%${dto.search}%` } },
                literal(`CAST("pets"."contract_number" AS TEXT) = '${dto.search}'`)
            ];
        }
        const clients = await this.repository.findAndCountAll({
            where,
            attributes: this.attributesAdmin,
            include: [{ model: Pets, attributes: ['id', 'name', 'birth_date', 'gender', 'contract_number'] }],
            distinct: true,
            limit: +dto?.perPage || 40,
            offset: +dto?.perPage * (+dto?.page - 1) || 0,
            order: [['createdAt', 'DESC']],
        });
        return clients;
    }

I'm getting this error:

{
  "name": "SequelizeDatabaseError",
  "parent": {
    "length": 116,
    "name": "error",
    "severity": "ERROR",
    "code": "42P01",
    "position": "460",
    "file": "parse_relation.c",
    "line": "3608",
    "routine": "errorMissingRTE",
    "sql": "SELECT \"Clients\".*, \"pets\".\"id\" AS \"pets.id\", \"pets\".\"name\" AS \"pets.name\", \"pets\".\"birth_date\" AS \"pets.birth_date\", \"pets\".\"gender\" AS \"pets.gender\", \"pets\".\"contract_number\" AS \"pets.contract_number\" FROM (SELECT \"Clients\".\"id\", \"Clients\".\"name\", \"Clients\".\"info\", \"Clients\".\"ref_key\", \"Clients\".\"code\", \"Clients\".\"is_hidden\", \"Clients\".\"createdAt\" FROM \"clients\" AS \"Clients\" WHERE (\"Clients\".\"deleted_at\" IS NULL AND (\"Clients\".\"info\" LIKE '%f%' OR CAST(\"pets\".\"contract_number\" AS TEXT) = 'f')) ORDER BY \"Clients\".\"createdAt\" DESC LIMIT 20 OFFSET 0) AS \"Clients\" LEFT OUTER JOIN \"pets\" AS \"pets\" ON \"Clients\".\"id\" = \"pets\".\"client_id\" AND (\"pets\".\"deleted_at\" IS NULL) ORDER BY \"Clients\".\"createdAt\" DESC;"
  },
  "original": {
    "length": 116,
    "name": "error",
    "severity": "ERROR",
    "code": "42P01",
    "position": "460",
    "file": "parse_relation.c",
    "line": "3608",
    "routine": "errorMissingRTE",
    "sql": "SELECT \"Clients\".*, \"pets\".\"id\" AS \"pets.id\", \"pets\".\"name\" AS \"pets.name\", \"pets\".\"birth_date\" AS \"pets.birth_date\", \"pets\".\"gender\" AS \"pets.gender\", \"pets\".\"contract_number\" AS \"pets.contract_number\" FROM (SELECT \"Clients\".\"id\", \"Clients\".\"name\", \"Clients\".\"info\", \"Clients\".\"ref_key\", \"Clients\".\"code\", \"Clients\".\"is_hidden\", \"Clients\".\"createdAt\" FROM \"clients\" AS \"Clients\" WHERE (\"Clients\".\"deleted_at\" IS NULL AND (\"Clients\".\"info\" LIKE '%f%' OR CAST(\"pets\".\"contract_number\" AS TEXT) = 'f')) ORDER BY \"Clients\".\"createdAt\" DESC LIMIT 20 OFFSET 0) AS \"Clients\" LEFT OUTER JOIN \"pets\" AS \"pets\" ON \"Clients\".\"id\" = \"pets\".\"client_id\" AND (\"pets\".\"deleted_at\" IS NULL) ORDER BY \"Clients\".\"createdAt\" DESC;"
  },
  "sql": "SELECT \"Clients\".*, \"pets\".\"id\" AS \"pets.id\", \"pets\".\"name\" AS \"pets.name\", \"pets\".\"birth_date\" AS \"pets.birth_date\", \"pets\".\"gender\" AS \"pets.gender\", \"pets\".\"contract_number\" AS \"pets.contract_number\" FROM (SELECT \"Clients\".\"id\", \"Clients\".\"name\", \"Clients\".\"info\", \"Clients\".\"ref_key\", \"Clients\".\"code\", \"Clients\".\"is_hidden\", \"Clients\".\"createdAt\" FROM \"clients\" AS \"Clients\" WHERE (\"Clients\".\"deleted_at\" IS NULL AND (\"Clients\".\"info\" LIKE '%f%' OR CAST(\"pets\".\"contract_number\" AS TEXT) = 'f')) ORDER BY \"Clients\".\"createdAt\" DESC LIMIT 20 OFFSET 0) AS \"Clients\" LEFT OUTER JOIN \"pets\" AS \"pets\" ON \"Clients\".\"id\" = \"pets\".\"client_id\" AND (\"pets\".\"deleted_at\" IS NULL) ORDER BY \"Clients\".\"createdAt\" DESC;",
  "parameters": {}
}

But when I remove the "limit", everything works properly.

What could be the problem, how can it be solved?

I solved this problem by using "subQuery: false", but this is a temporary solution as pagination is not working correctly.

0

There are 0 best solutions below