gaurrus
gaurrus

Reputation: 136

Sequelize issue interting rows on tables with triggers

I'm currently working with SQL Server and Sequelize on a project. I'm facing a problem when trying to insert rows in a table that has a trigger associated with it.

I was able to find this information about the problem https://github.com/sequelize/sequelize/issues/3284

I have downloaded the .js files and what it says is that it works with a temp table to overcome this issue:

SequelizeDatabaseError: The target table 'tableA' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.

This solution is not working for me, I have issues when trying to re use the object that were interseted and can't access to all of the variables.

Is there any other walk around for this issue?

This is my code:

  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");
});

When using the solution provided in the link above, it says that it can't find the 'id' property. 'id' is defined in Person and Student as the primary key.

Any ideas or official solution from sequelize to work this issue?

Thanks!

Upvotes: 2

Views: 2750

Answers (1)

kaybee99
kaybee99

Reputation: 4744

Set hasTrigger to true when defining the table, and Sequelize will take care of this for you. For example:

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

Upvotes: 5

Related Questions