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

时间:2015-11-13 16:16:16

标签: mysql node.js postgresql transactions sequelize.js

以下是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);

这里我使用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();
        });
    });
}

这里我正在执行以下方法来测试事务性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.
});

执行时,我可以在控制台上看到以下输出日志:

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;

但是在数据库中,尽管整个事务的ROLLBACK,第一个自动“bmw”仍然被写入。

我使用PostgreSQL 9.3.10,Ubuntu,MySQL 5.5.46,sequelize 3.13.0& amp; 3.0.0

有没有人注意到代码中的错误或者它是一个错误......?

1 个答案:

答案 0 :(得分:0)

在输出日志中,我们可以看到两个事务,一个是bf8cb998-657b-49b7-b29e-957bcf770b40,另一个是Default。第一个是回滚的,第二个不回滚,而且是你插入的地方。

您尝试将事务传递给Create函数,但看起来Sequelize并没有得到它。在一些版本发布之前,交易的语法已经改变,你可以尝试进行交易:t'属性在第二个对象而不是第三个?像这样:

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