Skip to content

JSON columns returned differently depending on whether MySQL or MariaDB is used #8702

Closed

Description

@ivokund

What are you doing?

When loading json columns from the database, toJSON() behaves differently depending on whether MySQL or MariaDB is used

// connect
const sequelize = new Sequelize(process.env.MYSQL_URI);
await sequelize.authenticate();

// create schema
const model = sequelize.define('test', { jsonColumn: { type: Sequelize.JSON } });
await sequelize.sync({ force: true });

// create and fetch some data
await model.create({ id: 1, jsonColumn: {a: 1}});
const fetchedModel = await model.findById(1);

// output
console.log(fetchedModel.toJSON().jsonColumn);
console.log(typeof fetchedModel.toJSON().jsonColumn);

What do you expect to happen?

The json column should be returned as a json-decoded object

What is actually happening?

Output on 5.7.20-0ubuntu0.17.04.1 (correct):

{ a: 1 }
object

Output on 10.2.10-MariaDB-log (incorrect):

{"a":1}
string

Dialect: specified above
Sequelize version: 4.23.1
mysql2 version: 1.5.1
Tested with master branch: No

Activity

  1. ivokund commented on Nov 28, 2017

    @ivokund
    Author

    I also note that a quick-hack that fixes the issue is to define a manual getter in the column definition as this:

    {
        type: Sequelize.JSON,
        get () {
            return typeof this.getDataValue(field) === 'string' ? JSON.parse(this.getDataValue(field)) : this.getDataValue(field);
        },
    }

    But I assume that this is not supposed to be required?

  2. sushantdhiman commented on Nov 28, 2017

    @sushantdhiman
    Contributor

    I would like to clarify that Sequelize only supports MariaDB because they (used to?) follow MySQL implementation and maintain API compliance.

    If MariaDB and MySQL are diverging then I am afraid we cant fix this as there is no "real" MariaDB dialect in Sequelize

  3. stale commented on Feb 26, 2018

    @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. SimonSchick commented on May 9, 2018

    @SimonSchick
    Contributor

    @sushantdhiman I ran into this bug as well today, wouldn't it be possible to incorporate this workaround/getter into sequelize? It's only about 5 loc.

  5. jasalo commented on Oct 25, 2018

    @jasalo

    @ivokund Thanks for the code snippet! Just one thing to add, a missing argument on the get function:

    {
        type: Sequelize.JSON,
        get ( field ) {
            return typeof this.getDataValue(field) === 'string' ? JSON.parse(this.getDataValue(field)) : this.getDataValue(field)
        },
    }
  6. knoxcard commented on Dec 14, 2018

    @knoxcard
    Contributor

    dialect: 'mariadb' has been reintroduced back into Sequelize v5, so this can now be used to differ logic between MySQL/MariaDB.

  7. soryy708 commented on Oct 6, 2019

    @soryy708
    Contributor

    According to the manual, JSON is not supported for MariaDB dialect.

  8. papb commented on Oct 30, 2019

    @papb
    Member

    @soryy708 I am pretty sure that is a mistake in the docs, I will look into it

  9. reopened this on Oct 30, 2019
  10. added
    status: wip
    docs
    and removed on Oct 30, 2019
  11. self-assigned this
    on Oct 30, 2019
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

    docsstatus: wip

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions