Skip to content

Sequelize crash - mariaDB 10.5.4 & JSON field #12583

Closed

Description

@usb248

Issue Description

plantage
mariaDB 10.4 OK, but mariaDB 10.5.X crashes my app
image

What are you doing?

    data: {
      type: db.Sequelize.JSON,
      allowNull: false
    },
Store.findOne({ 
			where: { ... },
			limit: 1,
			attributes: [ 'data'], // is the json field
			include: [{
		        model: model2,
		        attributes: [...],
		        required: true,
		        include: [{
			     	model: model3,
			     	attributes: [...],
			     	required: true
			    }]
		    }]
		})

What do you expect to happen?

no crash ?

What is actually happening?

trying to JSON.parse on something wrong
https://github.com/sequelize/sequelize/blob/master/src/dialects/mariadb/query.js#L180-L185

Additional context

Add any other context or screenshots about the feature request here.

Environment

  • Sequelize version: [email protected]
  • Node.js version: v12.18.3
  • Operating System: debian 10
  • If TypeScript related: TypeScript version: no ts

Issue Template 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
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

EDIT:
After debugging, i noticed that row[modelField.fieldName] is already an object, so obviously, if sequelize tries to json.parse an object it throws this error.... please fix this bug.

Activity

  1. miladmeidanshahi commented on Aug 9, 2020

    @miladmeidanshahi

    same error

    server error SyntaxError: Unexpected token o in JSON at position 1
        at JSON.parse (<anonymous>)
        at /node_modules/sequelize/lib/dialects/mariadb/query.js:183:72
        at Array.map (<anonymous>)
        at Query.handleJsonSelectQuery (/node_modules/sequelize/lib/dialects/mariadb/query.js:182:21)
        at Query.formatResults (/node_modules/sequelize/lib/dialects/mariadb/query.js:127:12)
        at Query.run (/node_modules/sequelize/lib/dialects/mariadb/query.js:72:17)
        at processTicksAndRejections (internal/process/task_queues.js:93:5)
        at async /node_modules/sequelize/lib/sequelize.js:619:16
        at async MySQLQueryInterface.select (/node_modules/sequelize/lib/dialects/abstract/query-interface.js:938:12)
        at async Function.findAll (/node_modules/sequelize/lib/model.js:1753:21) {
    
    
  2. seally1186 commented on Aug 13, 2020

    @seally1186

    Looked into this a bit after encountering the same issue. It seems like this is coming from the mariadb connector itself. With 10.4, it returns the data as a string (thus Sequelize needs to parse it), whereas it's parsing the result with server 10.5.

    This test case would suggest that this behavior is intentional, though seemingly undocumented. It also shows a temporary workaround (which is also undocumented from what I can tell): we can pass autoJsonMap: false as a dialect option.

    const sequelize = new Sequelize(..., {
      dialectOptions: { autoJsonMap: false }
    });
  3. miladmeidanshahi commented on Sep 12, 2020

    @miladmeidanshahi

    @seally1186 doesn't worked 🤔

  4. ciknowles commented on Oct 16, 2020

    @ciknowles

    Found a work around. This function mentioned above can be corrected (I replaced the .prototype in the top level of my project). Only JSON.parse if object is a string.

    https://github.com/sequelize/sequelize/blob/master/src/dialects/mariadb/query.js#L180-L185

     if (modelField.type instanceof DataTypes.JSON) {
            //value is return as String, no JSON
            rows = rows.map(row => {
              if (row[modelField.fieldName] && (typeof row[modelField.fieldName] === 'string')) {
                //don't see much point of setting to null as JSON.parse throws exception if it doesn't work
                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;
            });
          }
  5. usb248 commented on Nov 7, 2020

    @usb248
    Author

    Solution : Encode your column to : utf8mb4_unicode_ci (was in utf8mb4_bin). Worked for me

  6. remoreichsoellner commented on Apr 10, 2021

    @remoreichsoellner

    Looked into this a bit after encountering the same issue. It seems like this is coming from the mariadb connector itself. With 10.4, it returns the data as a string (thus Sequelize needs to parse it), whereas it's parsing the result with server 10.5.

    This test case would suggest that this behavior is intentional, though seemingly undocumented. It also shows a temporary workaround (which is also undocumented from what I can tell): we can pass autoJsonMap: false as a dialect option.

    const sequelize = new Sequelize(..., {
      dialectOptions: { autoJsonMap: false }
    });

    Thanks! Works for me on Maria DB Server 10.5.9 Homebrew Mac (Local Install), hopefully also working on production Server.

  7. Arose-Niazi commented on Aug 3, 2021

    @Arose-Niazi

    If this is still open I tried the fixes above those did not work for me.

    const result = await logs.findAll({
                where: {
                    [Op.and]: [{ Rank: { [Op.lte]: req.user.Rank } }, { ...where }],
                },
                offset: skipRecords,
                limit: perPage,
                order: [["ID", "DESC"]], //Works properly without this line
            });
    

    As soon as I add this order line I get error.

    SyntaxError: Unexpected token o in JSON at position 1
        at JSON.parse (<anonymous>)
        at D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\dialects\mariadb\query.js:186:72
        at Array.map (<anonymous>)
        at Query.handleJsonSelectQuery (D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\dialects\mariadb\query.js:185:21)
        at Query.formatResults (D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\dialects\mariadb\query.js:130:12)
        at Query.run (D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\dialects\mariadb\query.js:74:17)        
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at async D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\sequelize.js:619:16
        at async MySQLQueryInterface.select (D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\dialects\abstract\query-interface.js:953:12)
        at async Function.findAll (D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\node_modules\sequelize\lib\model.js:1753:21)
        at async D:\Development\Projects\NodeJS\Chashma-Seed-Portal-API\routes\api\logs.js:35:24
    GET /api/logs 500 173.942 ms - 35
    

    Query generated by the above code is

    SELECT `ID`, `Date`, `User`, `Details`, `Rank`, `Changes` FROM `logs` AS `logs` WHERE (`logs`.`Rank` <= 3 AND (`logs`.`ID` LIKE '%%' OR `logs`.`Date` LIKE 'Invalid date' OR `logs`.`User` LIKE '%%' OR `logs`.`Details` LIKE '%%' OR `logs`.`Rank` LIKE '%%' OR `logs`.`Changes` LIKE '\"%%\"')) ORDER BY `logs`.`ID` DESC LIMIT 0, 10
    

    Which is perfect

  8. github-actions commented on Oct 31, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  9. Arose-Niazi commented on Oct 31, 2021

    @Arose-Niazi

    Leaving a comment to remove it from stale

  10. github-actions commented on Nov 18, 2021

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 6.10.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions