Validaiton on transactions

12 views
Skip to first unread message

Rodrigo Araujo

unread,
May 31, 2016, 1:55:56 AM5/31/16
to Sequelize
Hey guys,

I need some help here to clarify how I'm supposed to do this:

I have two models: Files and Courses. Files belongs to Course and has a CourseId field. At some point I needed to use transactions to validate both of them before inserting them, however, one of my validation rules for the File model verifies if the informed CourseId exists (it's useful in other cases where I don't use transactions) and this rule is flagged during the transaction because there's no CouseId during it, as we can all expect.

I think this could be fixed if I could dinamically disable the validation rule for the CourseId field, but apparently this is not possible yet. Another approach would be virtual fields, but this one is not quite "elegant".

How can I solve this?
Thanks!

Mark Lester

unread,
May 31, 2016, 6:56:50 AM5/31/16
to Sequelize
if this is a db level condition on a foreign key then no, you arent just going to able to drop it for a specific transaction i would think.
if it is a validation rule specced by you then you could pass a value in options to turn it off.

Mick Hansen

unread,
May 31, 2016, 7:06:41 AM5/31/16
to Mark Lester, Sequelize
Depending on the RDBMS it's possible in some cases to defer foreign key checks in a transaction.
--
Mick Hansen
@mhansendev
mhansen.io

Rodrigo Araujo

unread,
May 31, 2016, 10:25:53 AM5/31/16
to Sequelize
I manually created the validation rule, however I don't see in the docs how I can disable the validation in one field, at least not in ".create()". Apparently I can do that using the option "skip" on ".validate()". Do you know how can I do that?

Thanks

Rodrigo Araujo

unread,
May 31, 2016, 10:55:25 AM5/31/16
to Sequelize
Actually it works, my bad. I'm using "skip".  Thank you guys!
Reply all
Reply to author
Forward
0 new messages