简体   繁体   English

Sequelize事务执行回滚,但数据库不执行

[英]Sequelize transaction executes a rollback but database not

Here is a snapshot of the database Tables and constraints in PostgreSQL: 这是PostgreSQL中数据库表和约束的快照:

CREATE TABLE garage (
    garage_id integer NOT NULL,
    garage_name text,
    garage_description text
);

CREATE TABLE auto (
    auto_id serial PRIMARY KEY,
    auto_name text,
    auto_description text,
    auto_price numeric(20,2),
    auto_category text,
    garage_id integer
);

ALTER TABLE ONLY auto
    ADD CONSTRAINT auto_garage_id_fkey FOREIGN KEY (gerage_id)
    REFERENCES gerage(gerage_id);

Here I am defining the database objects in nodejs using Sequelize: 在这里,我使用Sequelize在nodejs中定义数据库对象:

var Auto = sequelize.define('auto', {
    auto_id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    auto_name: Sequelize.STRING,
    auto_description: Sequelize.STRING,
    auto_price: Sequelize.NUMERIC,
    auto_category: Sequelize.STRING,
    garage_id: Sequelize.INTEGER
}, {freezeTableName: true,
        tableName: "auto",
        timestamps: false,
        paranoid: false,
        underscored: true});

function createAutos(auto_1,auto_2){
    return sequelize.transaction().then(function(t){
        return Auto.create(auto_1, 
            {fields: ['auto_name', 'auto_description', 'auto_price', 'auto_category', 'garage_id']},
            {transaction: t}
        ).then(function(){ 
            return Auto.create(auto_2, 
                {fields: ['auto_name', 'auto_description', 'auto_price', 'auto_category', 'garage_id']},
                {transaction: t});
        }).then(function(){
            t.commit();
        }).catch(function(err){
            t.rollback();
        });
    });
}

Here I am executing the following method to test the transactional createAutos(): 在这里,我正在执行以下方法来测试事务性createAutos():

createAutos({
    "auto_name": 'bmw',
    "auto_description": 'sport',
    "auto_price":4.95,
    "auto_category": 'luxes',
    "garage_id": 1 // Exists in the database
},{
    "auto_name": 'SSSS',
    "auto_description": 'sport',
    "auto_price":4.95,
    "auto_category": 'luxes',
    "garage_id": 200 // Doesn't exist in the database.
});

When executed, I can see the following output log on the console: 执行后,我可以在控制台上看到以下输出日志:

Executing (bf8cb998-657b-49b7-b29e-957bcf770b40): START TRANSACTION;
Executing (bf8cb998-657b-49b7-b29e-957bcf770b40): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (bf8cb998-657b-49b7-b29e-957bcf770b40): SET autocommit = 1;
Executing (default): INSERT INTO "auto" ("auto_name","auto_description","auto_price","auto_category","garage_id") VALUES ('bmw','sport',4.95,'luxes',1) RETURNING *;
Executing (default): INSERT INTO "auto" ("auto_name","auto_description","auto_price","auto_category","garage_id") VALUES ('SSSS','sport',4.95,'luxes',200) RETURNING *;
Executing (bf8cb998-657b-49b7-b29e-957bcf770b40): ROLLBACK;

But in the database the first auto "bmw" gets written despite the ROLLBACK of the whole transaction. 但是,尽管整个事务都回滚了,但在数据库中仍然写入了第一个自动“ bmw”。

I tested the program with PostgreSQL 9.3.10, Ubuntu, MySQL 5.5.46, sequelize 3.13.0 & 3.0.0 我使用PostgreSQL 9.3.10,Ubuntu,MySQL 5.5.46,sequelize 3.13.0&3.0.0测试了该程序

Does anyone notice here a mistake in the code or is it a bug...? 有人在这里注意到代码中的错误还是错误...?

In your output log, we can see two transactions, one bf8cb998-657b-49b7-b29e-957bcf770b40 and the other Default. 在输出日志中,我们可以看到两个事务,一个是bf8cb998-657b-49b7-b29e-957bcf770b40,另一个是Default。 The first one is rollbacked, the second one is not, and that's where you insert. 第一个回滚,第二个不回滚,这就是您要插入的位置。

You try to pass the transaction to the Create function but it looks like Sequelize doesnt get it. 您尝试将事务传递给Create函数,但是Sequelize似乎无法获得它。 Some releases ago, the syntax for transactions was changed, can you try to put the 'transaction: t' property in the second object and not the third ? 在某些版本之前,事务的语法已更改,您是否可以尝试将'transaction:t'属性放在第二个对象而不是第三个对象中? Something like this: 像这样:

Auto.create(auto_1, 
        {fields: ['auto_name', 'auto_description', 'auto_price', 'auto_category', 'garage_id'],
        transaction: t}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM