Sequelize Migration - Create Trigger in PostgreSQL

4.1k views Asked by At

I need to create Trigger on one of my Table using Sequelize Migration in Node and PostgreSQL. I haven't found enough documentation around it.

Currently I am using sequelize version 3.23 and quite familiar with PostgreSQL triggers, but not able to find anything around migration of triggers.

I am following docs provided on sequelize site related to migrations:

  module.exports = {
  up: function(queryInterface, Sequelize) {
    // create trigger
  },

  down: function(queryInterface, Sequelize) {
    // remove trigger
  }
}

Hope I get quick resolution around it.. Thanks in Advance :)

1

There are 1 answers

0
user3254198 On

You can add the triggers to your models, they're not called triggers in your sequelize models though they're called Hooks.

Using hooks is probably a better idea since you can integrate them with your models and create actual model instances but if you really want to use postgres triggers then you can use Sequelize.query() like so:

module.exports = {
  up: function(queryInterface, Sequelize) {
    queryInterface.sequelize.query('CREATE TRIGGER...')
  },

  down: function(queryInterface, Sequelize) {
    queryInterface.sequelize.query('DROP TRIGGER...')
  }
}