Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize Migration - Create Trigger in PostgreSQL

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 :)

like image 223
Viren Ajmera Avatar asked Oct 14 '25 13:10

Viren Ajmera


1 Answers

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...')
  }
}
like image 130
user3254198 Avatar answered Oct 17 '25 22:10

user3254198