node.jspostgresqlsequelize.jsjsonb-array-elements

Invalid field error on Sequelize bulkInsert method for Array of nested JSONB fields


I have a metadata column in my sequelize Notifications model

metadata: { type: DataTypes.ARRAY(DataTypes.JSONB) }

I want to do a bulkInsert for the notifications table using a seeder file but each time, I get an invalid field error.

below is my seeder code

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const notificationsData = [{
      id: 1,
      metadata: [
        {
          foo: 0,
          bar: 0.0014423,
          baz: {
            lorem: 987,
            ipsum: 'sample data',
            type: 'sample data',
            address: 'sample data'
          }
        },
        {
          foo: 10,
          bar: 2.423,
          baz: {
            lorem: 238,
            ipsum: 'sample data',
            type: 'sample data',
            address: 'sample data'
          }
        }
      ]
    }];
    await queryInterface.bulkInsert('Notifications', notificationsData, {});
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.bulkDelete('Notifications', null, {});
  },
};

On further research I discovered that sequelize has a parsing issue with JSON-related fields on bulkInsert and bulkCreate.

Is there any workaround for this?


Solution

  • I was able to fix the issue by modifying the bulkInsert script as follows

    await queryInterface.bulkInsert('Notifications', notificationsData, {}, {
       metadata: { type: Sequelize.ARRAY(Sequelize.JSONB) }
    });