ibywaks/cookbook

Deadlock error

Opened this issue · 1 comments

When I run the server, the tables are created in my local Mac MySQL database, but then I get this error:

SequelizeDatabaseError: Deadlock found when trying to get lock; try restarting transaction
    at Query.formatError (/Users/rowang/Documents/cookbook-github/node_modules/sequelize/lib/dialects/mysql/query.js:265:16)
    at Query.run (/Users/rowan/Documents/cookbook-github/node_modules/sequelize/lib/dialects/mysql/query.js:77:18)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at /Users/rowang/Documents/cookbook-github/node_modules/sequelize/lib/sequelize.js:619:16
    at Function.sync (/Users/rowang/Documents/cookbook-github/node_modules/sequelize/lib/model.js:1349:11)
    at async Promise.all (index 2)

Any ideas?

rmkane commented

In init.ts, the ALTER statements are issued here:

const dbInit = () =>
  Promise.all([
    Tag.sync({ alter: isDev || isTest }),
    Ingredient.sync({ alter: isDev || isTest }),
    Recipe.sync({ alter: isDev || isTest }),
    Review.sync({ alter: isDev || isTest }),
    RecipeTag.sync({ alter: isDev || isTest }),
    RecipeIngredient.sync({ alter: isDev || isTest }),
  ])

Also, this will need to be changed:

const isTest = process.env.NODE_ENV === 'test'  // This was incorrectly triple-not-equals

If you set NODE_ENV to production (not test nor development), the server will start up.

I assume the deadlock occurs because the table sync is happening in parallel, since they are all wrapped in a Promise.all. Maybe each sync needs to be performed in sequence. I would probably await each of them.