Sequelize Migration - 在 PostgreSQL 中创建触发器 [英] Sequelize Migration - Create Trigger in PostgreSQL

查看:108
本文介绍了Sequelize Migration - 在 PostgreSQL 中创建触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Node 和 PostgreSQL 中使用 Sequelize Migration 在我的一个表上创建触发器.我还没有找到足够的相关文档.

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.

目前我使用的是 sequelize 3.23 版,对 PostgreSQL 触发器非常熟悉,但无法找到有关触发器迁移的任何内容.

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

我正在关注 sequelize 网站上提供的与迁移相关的文档:

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

推荐答案

您可以将触发器添加到您的模型中,它们在您的续集模型中不被称为触发器,尽管它们被称为 Hooks.

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

使用钩子可能是一个更好的主意,因为您可以将它们与您的模型集成并创建实际的模型实例,但如果您真的想使用 postgres 触发器,那么您可以像这样使用 Sequelize.query():

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...')
  }
}

这篇关于Sequelize Migration - 在 PostgreSQL 中创建触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆