Reputation: 31
Here is a snapshot of the database Tables and constraints in 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:
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({
"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.
I tested the program with 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...?
Upvotes: 3
Views: 2418
Reputation: 1176
In your output log, we can see two transactions, one bf8cb998-657b-49b7-b29e-957bcf770b40 and the other 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. 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 ? Something like this:
Auto.create(auto_1,
{fields: ['auto_name', 'auto_description', 'auto_price', 'auto_category', 'garage_id'],
transaction: t}
Upvotes: 1