profile
viewpoint

Ask questionsHow use concat with alias model in sequelize

I am trying to perform a search based on the concatenation of 3 attributes of my model For this I am using the concat function but because my model has multiple relationships I had to use an alias for my model but at the time of listing the results I always get the following error:

error: an entry for the "cliente" table is missing in the clause

Stack:

  • NestJS : ^6.0.0
  • Sequelize-Typescript: ^0.6.11
  • Postgresql: 11

My query:

async search(criterio: string) {
try {
    let querybase: IFindOptions < Bill > ;
    const wheres: any = [];
    querybase = {
        include: [{
            model: Persona,
            as: 'cliente',
        }],
    }
    const wheres = [];
    if (criterio != null) {
        const arrayCriterio = [{
            [Op.or]: [
                Sequelize.where(
                    Sequelize.fn('concat', Sequelize.col('cliente.apellido_paterno'), ' ', Sequelize.col('cliente.apellido_materno'), ' ', Sequelize.col('cliente.nombre')), {
                        [Op.iLike]: `%${criterio}%`,
                    },
                ),
                Sequelize.where(
                    Sequelize.fn('concat', Sequelize.col('cliente.apellido_paterno'), ' ', Sequelize.col('cliente.nombre'), ' ', Sequelize.col('cliente.apellido_materno')), {
                        [Op.iLike]: `%${criterio}%`,
                    },
                ),
                Sequelize.where(
                    Sequelize.fn('concat', Sequelize.col('cliente.apellido_materno'), ' ', Sequelize.col('cliente.apellido_paterno'), ' ', Sequelize.col('cliente.nombre')), {
                        [Op.iLike]: `%${criterio}%`,
                    },
                ),
                Sequelize.where(
                    Sequelize.fn('concat', Sequelize.col('cliente.apellido_materno'), ' ', Sequelize.col('cliente.nombre'), ' ', Sequelize.col('cliente.apellido_paterno')), {
                        [Op.iLike]: `%${criterio}%`,
                    },
                ),
                Sequelize.where(
                    Sequelize.fn('concat', Sequelize.col('cliente.nombre'), ' ', Sequelize.col('cliente.apellido_materno'), ' ', Sequelize.col('cliente.apellido_paterno')), {
                        [Op.iLike]: `%${criterio}%`,
                    },
                ),
                Sequelize.where(
                    Sequelize.fn('concat', Sequelize.col('cliente.nombre'), ' ', Sequelize.col('cliente.apellido_paterno'), ' ', Sequelize.col('cliente.apellido_materno')), {
                        [Op.iLike]: `%${criterio}%`,
                    },
                ),
            ],
        }, ];
        wheres.push(arrayCriterio);
    }
    querybase.where = {
        [Op.and]: wheres,
    };
    const clientes = await this.bill.findAll(querybase);
    return clientes;      
} catch (error) {
    return error;
}

}

RobinBuschmann/sequelize-typescript

Answer questions RobinBuschmann

Hey @AndresED, thanks for asking. This is a very specific question. Since sequelize-typescript is just on top of sequelize itself, I recommend to ask this question in the sequelize repo or stackoverflow.

useful!
source:https://uonfu.com/
Github User Rank List