Repository navigation
Open
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Description
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:
SQL Output 2:
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
Reacted by Ryan, MaximeMaillet, petru, Sello Mkantjwa and Douglas Nassif Roma Junior
Metadata
Metadata
Assignees
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Type
Projects
- StatusShow more project fieldsNo status


Activity
douglasjunior commentedon Jun 28, 2017
I was able to work with a workaround. But it is a very bad solution.
Instead of
separate:trueI used a subquery.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" } ] } ]douglasjunior commentedon Aug 13, 2017
Please, does anyone know if this is a bug?
stale commentedon Sep 22, 2017
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 🙂
douglasjunior commentedon Sep 22, 2017
The problem persists, just was not answered :(
friend0 commentedon Dec 8, 2017
I'm having the same issue with separate
edvaldoszy commentedon Feb 9, 2018
I'm facing the same problem
MarkusPint commentedon Feb 10, 2018
Same issue, but on PG
arielshad commentedon Jun 12, 2019
Same issue
yanqing6628780 commentedon Sep 8, 2020
same issue at v3.30.4
slaveofcode commentedon Dec 15, 2020
Is there any workaround on this issue? running version @^4.44.4 still an issue for me
tuankiet116 commentedon Jan 28, 2022
running version 6.1.13 still has this issue. Is there any answer?