Skip to content

MariaDB JSON DataType - included / Joined fields are not parsed (incl. fix #13633) #13646

Open

Description

@LukasKubec

Issue Creation Checklist

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

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

  1. added
    type: bug
    P1: important
    on Nov 10, 2021
  2. LukasKubec commented on Nov 10, 2021

    @LukasKubec
    Author

    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:

    // parsing extracted to its own method.
    // recoursion used for include in include -> is there .option & option.hasJoin in case of included include, or just .hasJoin?
    // field name -> if instance.as, then its probably included model and as is used? Not sure about this (different "as" with different assoc.)
    // no guarantees, but works for me.
      
    handleJsonSelectQuery(rows) {
        if (!this.model || !this.model.fieldRawAttributesMap) {
          return;
        }
        let recourseParsing = (instance, rows) => {
          if (instance.options && instance.options.hasJoin) {
            const included = this.options.include;
            for (let i = 0; i < included.length; i++) {
              rows = this.parseJson(included[i], rows);
              rows = recourseParsing(included[i], rows);
            }
          } else {
            rows = this.parseJson(instance, rows);
          }
          return rows; 
        }
        rows = recourseParsing(this, rows);
      }
      
      parseJson (instance, rows) {
        for (const _field of Object.keys(instance.model.fieldRawAttributesMap)) {
          const modelField = instance.model.fieldRawAttributesMap[_field];
          if(modelField.type instanceof DataTypes.JSON) {
            const fieldName = instance.as ? instance.as + '.' + modelField.fieldName : modelField.fieldName;
            rows = rows.map(row => {
              if (row[fieldName] && typeof row[fieldName] === 'string') {
                row[fieldName] = JSON.parse(row[fieldName]);
              }
              if (DataTypes.JSON.parse) {
                return DataTypes.JSON.parse(fieldName, this.sequelize.options,
                  row[fieldName]);
                }
              return row;
            })
          }
        }
        return rows;
      }
    
  3. 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

    P1: importanttype: bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions