Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize seeder: unexpected identifier error, no stack trace further explaining it

I was having an issue with Sequelize where running sequelize:seed for Postgres with autoIncrement key broke the ability to add new items to that table. Sequelize doesn't automatically sync the model with the latest id in the table, so I got duplicate key errors after adding seed data.

I was trying to fix it with some advice that I found here about resyncing the serial after the seed file runs, but I'm getting an Unexpected Identifier error and no further explanation.

Here's my code - can anyone tell what's wrong? My linter isn't throwing any errors so I don't think it's a standard syntax error:

'use strict';
const db = require('../models');

module.exports = {
  up: async (queryInterface, Sequelize) => {
    /*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkInsert('People', [{
        name: 'John Doe',
        isBetaMember: false
      }], {});
    */
    return await queryInterface
      .bulkInsert('user_podcasts', [
        {
          id: 1,
          userId: 1,
          podcastId: '88b15eefe35d42c58bca9c5e17080661',
          createdAt: new Date().toISOString(),
          updatedAt: new Date().toISOString()
        },
        {
          id: 2,
          userId: 1,
          podcastId: '7f519d33692246a98688a1415c3c591c',
          createdAt: new Date().toISOString(),
          updatedAt: new Date().toISOString()
        },
        {
          id: 3,
          userId: 2,
          podcastId: '7f519d33692246a98688a1415c3c591c',
          createdAt: new Date().toISOString(),
          updatedAt: new Date().toISOString()
        },
        {
          id: 4,
          userId: 2,
          podcastId: 'd9604d45a8494577bec068df875fb69d',
          createdAt: new Date().toISOString(),
          updatedAt: new Date().toISOString()
        }
      ])
      .then(async () =>
        db.query(
          `ALTER SEQUENCE "${
            model.user_podcasts
          }_id_seq" RESTART WITH ${(await model.count()) + 1}`
        )
      )
      .catch(error => {
        if (error.message.indexOf('already exists') > -1) return;
        console.log(error);
      });
  },

  down: (queryInterface, Sequelize) => {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkDelete('People', null, {});
    */
  }
};

like image 459
Ollie Khakwani Avatar asked Oct 22 '25 12:10

Ollie Khakwani


1 Answers

Use this command to get the error stack trace. Notice the --debug option.

sequelize-cli db:migrate --debug

If you have not installed sequelize-cli then, install it using the below command.

npm install sequelize-cli 

You can install it globally, if you want to, using -g option.

like image 76
mohammed_ayaz Avatar answered Oct 24 '25 06:10

mohammed_ayaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!