Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize throws generic error messages when querying the database

Sequelize version: 6.13.0
Database version: PostgreSQL 13.3

Whenever there's an issue with a query, Sequelize doesn't tell me the actual issue. For example, this old Stack Overflow post shows Sequelize displaying an error that a column is unknown. In my case, I receive a generic error message and need to manually inspect the queries and my database tables to find out what's wrong. Here's an example:

const user = await User.findByPk(params.id);

const settings = await Setting.findAll({ attributes: ['id', 'key', 'defaultValue'] });
const userSettings = await user.getUserSettings({ attributes: ['id', 'key', 'value'] });

This will produce the following SQL:

Executing (default): SELECT "id", "name", "email", "password", "referralCode", "referredByUserId", "bio", "twitterUrl", "facebookUrl", "instagramUrl", "createdAt", "updatedAt" FROM "Users" AS "User" WHERE "User"."id" = '1';
Executing (default): SELECT "id", "key", "defaultValue" FROM "Settings" AS "Setting";
Executing (default): SELECT "id", "key", "value" FROM "UserSettings" AS "UserSetting" WHERE "UserSetting"."userId" = 1;

And here's the error message:

/home/user/app/backend/node_modules/sequelize/lib/dialects/postgres/query.js:76
    const errForStack = new Error();
                        ^
Error: 
    at Query.run (/home/user/app/backend/node_modules/sequelize/lib/dialects/postgres/query.js:76:25)
    at /home/user/app/backend/node_modules/sequelize/lib/sequelize.js:642:28
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at PostgresQueryInterface.select (/home/user/app/backend/node_modules/sequelize/lib/dialects/abstract/query-interface.js:979:12)
    at Function.findAll (/home/user/app/backend/node_modules/sequelize/lib/model.js:1789:21)
    at HasMany.get (/home/user/app/backend/node_modules/sequelize/lib/associations/has-many.js:228:21)

I had to manually check my database tables, and I found out that the key column is not present on the UserSettings table.

Why doesn't Sequelize tell me this on any of my models? Is there some configuration I'm missing?

like image 642
Alexander Avatar asked Jun 29 '26 01:06

Alexander


1 Answers

As @Anatoly mentioned in the comments, I just needed to catch the errors in try-catch block, like this for example:

try {
  const user = await User.findByPk(params.id);

  const settings = await Setting.findAll({ attributes: ['id', 'key', 'defaultValue'] });
  const userSettings = await user.getUserSettings({ attributes: ['id', 'key', 'value'] });
} catch (e) {
  console.log(e);
  next(e);
}

Coming from a Ruby on Rails background, I had expected the errors to be shown in the output of my terminal running the Express server, but that doesn't seem to be the case.

Thanks @Anatoly, debugging will be a lot easier now!

like image 155
Alexander Avatar answered Jul 01 '26 19:07

Alexander



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!