0

I'm having problems in creating a trigger using Node.js + Express + Sequelize.

I have tables for my model objects (i.e. User, Device, ...) and join tables to express their Many-to-Many associations (i.e. hasDevice).

My troubles come from my inability in creating a simple trigger with a

sequelize.query('query', null, {raw: true});

since tables creation is totally asynchronous and I cannot know when trigger's table will be created (In particular I'm getting the following error:

Possibly unhandled SequelizeDatabaseError: ER_NO_SUCH_TABLE: Table 'express612.hasDevice' doesn't exist

Thanks in advance for your help.

4
  • You need to run the trigger that creates the query after the tables are created. What is the code that asynchronously creates the tables? Commented Dec 28, 2014 at 16:45
  • That is my problem indeed. I don't feel like I have control over table creation process. I'm following this tutorial sequelizejs.com/articles/express#the-application and, as you can see, there's no explicit section regarding table creation (or maybe it's just me being a little newbie on this matter) Commented Dec 28, 2014 at 16:54
  • 1
    Seems like it would be in the .then callback after .sync Commented Dec 28, 2014 at 16:57
  • You were goddamn right :) In particular, according to Sequelize's official tutorial, sync() is used inside ./bin/www (and that's why I wasn't seeing it). @ExplosionPills you can write an answer to receive the green tick :) Commented Dec 28, 2014 at 17:06

1 Answer 1

1

The tables are created asynchronously, so you need to do any work that relies on the creation of the tables (such as trigger creation) in the callback after tables are created. According to the documentation, this seems to be after the .sync method, for example:

http://sequelizejs.com/articles/express#the-application

You can create the trigger in the callback to .sync().then

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.