【问题标题】:Sequelize issue interting rows on tables with triggersSequelize 使用触发器在表上插入行的问题
【发布时间】:2016-04-27 15:06:21
【问题描述】:

我目前正在使用 SQL Server 和 Sequelize 处理一个项目。尝试在具有关联触发器的表中插入行时遇到问题。

我能够找到有关问题https://github.com/sequelize/sequelize/issues/3284的此信息

我已经下载了 .js 文件,它说它可以与临时表一起使用来解决这个问题:

SequelizeDatabaseError:如果该语句包含没有 INTO 子句的 OUTPUT 子句,则 DML 语句的目标表“tableA”不能有任何启用的触发器。

此解决方案对我不起作用,我在尝试重新使用已插入且无法访问所有变量的对象时遇到问题。

对于这个问题还有其他的解决方法吗?

这是我的代码:

  return Person.create( {   lastName: options.lastName,
                            name: options.name,
                         }, {transaction: t}).then(
                                function (person) {
                                        return Student.create({  id                 :person.id,
                                                                numberRecord              :'PF-'+person.id,

                                                            }, {transaction: t}).then(function (student) {
                                                return Shoots.create({  id                  : person.id,
                                                                            numberRecord              :'PF-'+person.id
                                                    }, {transaction: t}).then(function (record) {



                                                            return StudentContract.create({
                                                                PersonID              :person.id,
                                                                dateStart:'2016021 00:00:00.000',
                                                                endDate:'20161231 00:00:00.000',}
                                                                , {transaction: t}).then(function (contract) {
                                                                return student;

                                                            });

                                                    });
                                        });

                                        });

        }).then(function (result) {
    // Transaction has been committed
    // result is whatever the result of the promise chain returned to the transaction callback is
    console.log(result);
    cb(null, result);
}).catch(function (err) {
    // Transaction has been rolled back
    // err is whatever rejected the promise chain returned to the transaction callback is
    console.error(+err);
    cb(err, "Transaction Failed");
});

使用上面链接中提供的解决方案时,它说找不到“id”属性。 'id' 在 Person 和 Student 中定义为主键。

sequelize 有什么想法或官方解决方案来解决这个问题吗?

谢谢!

【问题讨论】:

  • 我忘了提到启用触发器的表是最后一个:StudentContract。谢谢

标签: sql-server node.js triggers sequelize.js


【解决方案1】:

在定义表格时将hasTrigger 设置为true,Sequelize 会为您处理这个问题。例如:

sequelize.define(TABLE_NAME,
{
  id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    primaryKey: true
  },
  description: {
    type: Sequelize.VARCHAR(8000)
  }
},
{
  // options
  timestamps: false,
  hasTrigger: true
});

【讨论】:

  • 在续集上,这在进行软删除时不起作用..我仍然在寻找如何在进行软删除时防止输出子句
【解决方案2】:

我已阅读您的问题,并且知道您需要捕获插入的内容。是的,可以使用表变量捕获插入的数据。

试试下面,希望它至少能帮助你获得一些其他的想法。

    DECLARE @IDENTITY_CAPTURE TABLE (INSERTED_IDENTITY BIGINT, INSERTED_BASE_NME VARCHAR(20))

    INSERT INTO SALESORDER  
    OUTPUT INSERTED.SALESORDER_NBR, INSERTED.SALESORDER_NME  INTO @IDENTITY_CAPTURE
    SELECT 1, 'PICSART'
    UNION ALL
    SELECT 2, 'IMAX'

然后您可以查询 Table 变量以获取插入的数据。

谢谢

【讨论】:

  • 很抱歉没有收到,但我应该在哪里添加您发布的此声明?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 2019-09-17
  • 2012-09-13
  • 2014-06-19
相关资源
最近更新 更多