我想实现soft_delete。因此,我在模型类beforeBulkDelete
中创建了一个钩子,该钩子调用了一个存储过程,该存储过程将某个日期放在deleted_at
列中并返回TRIGGER
。
我想返回NULL
作为触发器,因此不会发生实际的删除。
我尝试使用CREATE TRIGGER
命令而不是使用Sequelize挂钩来创建触发器,并且效果很好。但是我想用钩子来做。
这是我的模型课-
```export default function defineAttachment(
sequelize: Sequelize.Sequelize,
DataTypes: any
) {
const XerceCaseAttachment = sequelize.define(
'XerceCaseAttachment',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
.
.
.
deletedAt: {
type: DataTypes.DATE,
field: 'deleted_at',
},
},
{
tableName: 'attachments',
hooks: {
beforeBulkDestroy: (options: any) => {
sequelize.query(`SELECT soft_delete();`, {
type: sequelize.QueryTypes.DELETE,
transaction: options.transaction,
});
},
},
}
);```
这是我的soft_delete
存储过程-
```CREATE OR REPLACE FUNCTION soft_delete()
RETURNS TRIGGER AS
$$
BEGIN
RAISE NOTICE 'test triggers';
EXECUTE 'UPDATE attachments SET deleted_at = current_timestamp WHERE id = $1';
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;```
我遇到以下错误-
```Unhandled rejection SequelizeDatabaseError: trigger functions can only be called as triggers
at Query.formatError (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/sequelize/lib/dialects/postgres/query.js:363:16)
at query.catch.err (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
at bound (domain.js:395:14)
at runBound (domain.js:408:12)
at tryCatcher (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
at process.topLevelDomainCallback (domain.js:120:23)```
期望-
它不应删除该行,而是将某些日期存储在delete_at