What is the right way to make transactions on sequelize?

10,135 views
Skip to first unread message

Norman Enmanuel

unread,
Dec 5, 2014, 3:09:46 AM12/5/14
to sequ...@googlegroups.com
Hi everyone, i'm having troubles with the transaction promises styles, the style of promises change fast and the documentation is the same (old), do you have a piece of example to make transactions? using commit and rollback easy like to store an object thanks.

Here something else:
I read this link to follow but this fail http://sequelizejs.com/docs/latest/misc
And this is the example of transaction on sequelize documentation: http://sequelizejs.com/docs/1.7.8/transactions#block-0-line-19

Mick Hansen

unread,
Dec 5, 2014, 3:32:47 AM12/5/14
to Norman Enmanuel, sequ...@googlegroups.com
We've updated the documentation so take a look at http://sequelize.readthedocs.org/en/latest/docs/transactions/ and if that still doesn't work for you we can take it from there.
--
Mick Hansen
@mhansendev
mhansen.io

Norman Enmanuel

unread,
Dec 5, 2014, 8:24:01 AM12/5/14
to sequ...@googlegroups.com, norman...@gmail.com
Hi thanks for the answer, was very helpfully and it works fine, but now i have problems with the commit, let me explain a bit, i have to create 3 objects then with those object i need yours id to make the last insert because is a table M2M, i would appreciate if you give me some article to commit manual.

I see this but i'm not sure if i have to modify the api 'Sequelize' object.

Mick Hansen

unread,
Dec 5, 2014, 8:52:54 AM12/5/14
to Norman Enmanuel, sequ...@googlegroups.com
Hi Norman,

You'll have to show some of your code for me to comment on it.
If you use Sequelize relations you don't need insert the id on your own. And even if you do you don't need to use manual commit.
And even then, try looking at the resource again, manual commit is the second section on there :)

Norman Enmanuel

unread,
Dec 5, 2014, 9:16:03 AM12/5/14
to sequ...@googlegroups.com, norman...@gmail.com

Sure, this is a piece of code to create an user, that i want is commit after create the second model because i'm using this to validate the data from a form.

So, when i run this code always create the object on db:

if you have some problem why i use your info tell me to remove images, is just an example.

Mick Hansen

unread,
Dec 5, 2014, 9:42:31 AM12/5/14
to Norman Enmanuel, sequ...@googlegroups.com
You can just remove the t.commit() part to when you want to commit, there's no requirement.
Look at the example from the site, it does 2 dependent calls before committing manually.

Norman Enmanuel

unread,
Dec 5, 2014, 11:40:44 PM12/5/14
to sequ...@googlegroups.com, norman...@gmail.com
Hi again Mick and thanks for your answer but always it commit, i want submit this but when i call t.commit(); the problem is that t.commit(); always is called even if i set the option (model, {data:1, data2: 'etc'}, {transaction: t, autocommit: false}). it doesn't works.

Mick Hansen

unread,
Dec 6, 2014, 6:41:47 AM12/6/14
to Norman Enmanuel, sequ...@googlegroups.com
The autocommit option has nothing to do with actually committing, it sets the sql auto commit option.
To turn off the Sequelize based auto committing, you have to use the non auto resolve commit like i told you, i.e. use the transaction from a promise, not from a callback.

Norman Enmanuel

unread,
Dec 6, 2014, 4:32:45 PM12/6/14
to sequ...@googlegroups.com, norman...@gmail.com
Hello again Mick Hansen, thanks again for your asnwer, i follow your advices but i don't know what is the problem, i leave you 2 codes to make an commit until email is ok, in resume it have to do: "first make a query in db to know If employee not exists in db so if this exists i send a note to the client using json with something like hey "There are an employee with this name", so if the user not exists in db i use the method of sequelize create to create de employee but the thing here is that i really don't want insert yet, i will insert it, but later, so imagine that i insert but the transaction is yet waiting for the t.commit(); using this logic i can get the id of employee and use later, at this point, i do a new query on db to check if exists an email with the value provided by form html, if the email exist i send a new response json telling hey "There are an employee with this email", if the email doesn't exists i proceed to create using again the method create of sequelize.


In resume the idea to use transactions is first pass al 4 validations an then commit if amid of all the caos occurs something bad i rollback the transaction and i will be happy, but not working, Sequelize in my opinion is the best framework to work with databases relationals like MySQL, PosgreSQL and SQLite and this point of transaction have a behavior weird or in better opinion seems like don't work like charm.

Another point is, i think you are an employee or something of the team Sequelize and iknow that i don't have to tell you everything about what i'm doing with this, and i tell you this because in the documentations there are 2 examples but 1 of those examples are bad, look this code: 

If people run this code will throw an exception.

If you think that i'm wrong about the concepts of transaction tell me, i know that you also works, i don't want to be a headache for you also my english is not perfect because my language is spanish, so take me some patience thus as I have of you.

Thanks.

Mick Hansen

unread,
Dec 7, 2014, 8:14:20 AM12/7/14
to Norman Enmanuel, sequ...@googlegroups.com
You are still using callback style while expecting to be able to commit/rollback manually.
If you want to commit/rollback manually you must use promise style, sequelize.transaction().then(function (t) {}); rather than sequelize.transaction(function (t) {});

Norman Enmanuel

unread,
Dec 7, 2014, 9:00:49 AM12/7/14
to sequ...@googlegroups.com, norman...@gmail.com
Thanks you now i understand better, and documentation is well, regards.
Reply all
Reply to author
Forward
0 new messages