Skip to content

findAll and separate:true not work with hasMany and constrants:false #7856

Open

Description

@douglasjunior

I'm trying to do a hasMany query with separate:true without using constraints. But the relationship comes empty. With separate: false works fine.

Query:

    PlacaCliente.findAll({
        attributes: ["id", "placa"],
        include: [{
            model: ClienteLog,
            attributes: ["id", "placa"],
            separate: true
        }]
    }).then((result) => {
        res.json(result);
    }).catch((ex) => {
        next(ex);
    })
PlacaCliente model:

function associate(models) {
    let PlacaCliente = models["placa_cliente"];
    let ClienteLog = models["cliente_log"];

    PlacaCliente.hasMany(ClienteLog, {
        foreignKey: "placa",
        sourceKey: "placa",
        constraints: false,
        onUpdate: "NO ACTION",
        onDelete: "NO ACTION"
    });
}

    var Model = sequelize.define("placa_cliente",
        {
            id: {
                type: DataTypes.INTEGER,
                primaryKey: true
            },
            placa: DataTypes.STRING(30),
            ...
       }, {
            classMethods: {
                associate: associate
            }
        });


ClienteLog model:

function associate(models) {
    let PlacaCliente = models["placa_cliente"];
    let ClienteLog = models["cliente_log"];

    ClienteLog.belongsTo(PlacaCliente, {
        foreignKey: "placa",
        targetKey: "placa",
        constraints: false,
        onUpdate: "NO ACTION",
        onDelete: "NO ACTION"
    });
}

    var Model = sequelize.define("cliente_log",
        {
            id: {
                type: DataTypes.INTEGER,
                primaryKey: true
            },
            placa: DataTypes.STRING(30),
            ...
        }, {
            classMethods: {
                associate: associate
            }
        });


Generated SQLs:

Executing (default): SELECT `placa_cliente`.`id`, `placa_cliente`.`placa` FROM `placa_cliente` AS `placa_cliente`;
Executing (default): SELECT `id`, `placa` FROM `cliente_log` AS `cliente_log` WHERE `cliente_log`.`placa` IN ('0-30-192-226-224-232', '216-128-57-37-209-56', '216-128-57-37-207-245', '216-128-57-37-209-59');

JSON Output: (cliente_logs are empty)

[
    {
        "id": 1,
        "placa": "0-30-192-226-224-232",
        "cliente_logs": [
            {}
        ]
    },
    {
        "id": 167,
        "placa": "216-128-57-37-209-56",
        "cliente_logs": [
            {}
        ]
    },
    {
        "id": 195,
        "placa": "216-128-57-37-207-245",
        "cliente_logs": [
            {}
        ]
    },
    {
        "id": 199,
        "placa": "216-128-57-37-209-59",
        "cliente_logs": [
            {}
        ]
    }
]

SQL Output 1:

image

SQL Output 2:

image

Dialect: "mysql": "^2.13.0" / "mysql2": "^1.3.5"
Database version: 5.7.14
Sequelize version: "sequelize": "^3.30.4" / "sequelize": "^4.2.0"

With sequelize 4 the association has changed to Model.associate = associate

Activity

  1. douglasjunior commented on Jun 28, 2017

    @douglasjunior
    Author

    I was able to work with a workaround. But it is a very bad solution.

    Instead of separate:true I used a subquery.

        const lastClienteLogSQL =
            sequelize.dialect.QueryGenerator.selectQuery('cliente_log', {
                attributes: ["id"],
                where: {
                    placa: {
                        $eq: sequelize.col('placa_cliente.placa')
                    }
                },
                order: [
                    ['id', 'DESC']
                ],
                limit: 1
            }).slice(0, -1);
    
        PlacaCliente.findAll({
            attributes: ["id", "placa"],
            include: [{
                model: ClienteLog,
                attributes: ["id", "placa"],
                required: true,
                where: {
                    id: {
                        $eq: sequelize.literal(`(${lastClienteLogSQL})`)
                    }
                }
            }]
        }).then((clientes) => {
            res.json(clientes);
        }).catch((ex) => {
            next(ex);
        })

    Out:

    [
        {
            "id": 1,
            "placa": "0-30-192-226-224-232",
            "cliente_logs": [
                {
                    "id": 1593108,
                    "placa": "0-30-192-226-224-232"
                }
            ]
        },
        {
            "id": 167,
            "placa": "216-128-57-37-209-56",
            "cliente_logs": [
                {
                    "id": 1593107,
                    "placa": "216-128-57-37-209-56"
                }
            ]
        },
        {
            "id": 195,
            "placa": "216-128-57-37-207-245",
            "cliente_logs": [
                {
                    "id": 1593110,
                    "placa": "216-128-57-37-207-245"
                }
            ]
        },
        {
            "id": 199,
            "placa": "216-128-57-37-209-59",
            "cliente_logs": [
                {
                    "id": 1593111,
                    "placa": "216-128-57-37-209-59"
                }
            ]
        }
    ]
  2. douglasjunior commented on Aug 13, 2017

    @douglasjunior
    Author

    Please, does anyone know if this is a bug?

  3. stale commented on Sep 22, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  4. douglasjunior commented on Sep 22, 2017

    @douglasjunior
    Author

    The problem persists, just was not answered :(

  5. friend0 commented on Dec 8, 2017

    @friend0

    I'm having the same issue with separate

  6. edvaldoszy commented on Feb 9, 2018

    @edvaldoszy

    I'm facing the same problem

  7. added
    type: bug
    on Feb 9, 2018
  8. MarkusPint commented on Feb 10, 2018

    @MarkusPint

    Same issue, but on PG

  9. arielshad commented on Jun 12, 2019

    @arielshad

    Same issue

  10. yanqing6628780 commented on Sep 8, 2020

    @yanqing6628780

    same issue at v3.30.4

  11. slaveofcode commented on Dec 15, 2020

    @slaveofcode

    Is there any workaround on this issue? running version @^4.44.4 still an issue for me

  12. tuankiet116 commented on Jan 28, 2022

    @tuankiet116

    running version 6.1.13 still has this issue. Is there any answer?

  13. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions