Repository navigation
Open
Open
Copy link
Labels
Description
Issue Creation Checklist
- I have read the contribution guidelines
Bug Description
On Find queries with MariaDB, Sequelize currently have issue with parsing JSON datatype columns. That is fixed by #13633 which works just fine, but only for columns on the top level model. If any other model is joined via include:, then JSON columns in included model are not parsed. I had chance to check it only with eager loading.
lib/dialects/mariadb/query.js in this function (line 177+), this.model does not include fields of joined models (they are available in this.options.include? if this.options.hasJoin is true) - this is version of fnction from fix #13633
handleJsonSelectQuery(rows) {
if (!this.model || !this.model.fieldRawAttributesMap) {
return;
}
for (const _field of Object.keys(this.model.fieldRawAttributesMap)) {
const modelField = this.model.fieldRawAttributesMap[_field];
if (modelField.type instanceof DataTypes.JSON) {
// Value is returned as String, not JSON
rows = rows.map(row => {
if (row[modelField.fieldName] && typeof row[modelField.fieldName] === 'string') {
row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]);
}
if (DataTypes.JSON.parse) {
return DataTypes.JSON.parse(modelField, this.sequelize.options,
row[modelField.fieldName]);
}
return row;
});
}
}
}so if:
const A = sequelize.define('A', {
AjsonField: { DataType.JSON },
});
const B = sequelize.define('B', {
BjsonField: { DataType.JSON },
});
A.hasMany(B);
// A['B.BjsonField'] will **not** be parsed to JSON
// A.AjsonField **will be** parsed to JSON
A.findAll({include: B})
// B.BjsoinField now **will be** parsed to JSON
B.find();What do you expect to happen?
Fields of included models should be parsed to JSON as well in case of DataType.JSON
What is actually happening?
String is returned - e.g. this from DB:
{"key": value}is returned as:
"{\"key\": value}"
in JSON fields of included models.
Environment
- Sequelize version: 6.9.0. + fix Fix MariaDB 10.5 JSON #13633
- Node.js version: 14.7.5.
Bug Report Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
- I think this problem happens only for the following dialect(s): MariaDB
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
LukasKubec commentedon Nov 10, 2021
I made a fix for myself, but: Im quite a rookie. Works for me, not sure if will work the same way with e.g. only one include (I have "include: all" case) or .get loading of joins OR includes in includes OR ... . Beside name of the field, its the same logic as with the json parsing on top-level model, I just used the same method for all. If recursion used, then if instance.hasjoin (? so its include in include?), do parseJson.
code if someone finds it useful: