0

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?

1
  • 1
    Isn't enough two workarounds in the mentioned issue? Commented Oct 2, 2023 at 17:53

1 Answer 1

0

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) }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.