Sailsjs many-to-many associations using non-transactional save -
Sailsjs many-to-many associations using non-transactional save -
i'm happily using beta v0.10 of sailsjs , worrying 1 question regarding many-to-many associations:
say, model-a , model-b have many-to-many-association via 2 attributes. api recommends using "add"-method append 1 existing model-a-object collection of model-b-object, followed phone call "save"-method of model-b, create alter persistant. works expected, documentation mentions next save-method:
"this instance method. currently, instance methods not transactional. because of this, recommended utilize equivalent model method instead."
but unfortunately, phone call model-b-update not persist changes in collections in many-to-many relationship. not expert in databases, insecure if bug, feature or misunderstanding on side. suggestions welcome!
ben
edit: here more detailed description of issue:
say, have 2 models, user , group:
/** * user.js * module.exports = { attributes: { [...] memberofgroups: { collection: 'group', via: 'members' } }; /** * group.js * module.exports = { attributes: { [...] members: { collection: 'user', via: 'memberofgroups', dominant: true } } };
as can see, there many-to-many association between these 2 models. assume, there 2 instances, 1 of each model. 1 called aliceuser, other publicgroup. want add together aliceuser fellow member publicgroup.
when do
publicgroup.members.add(aliceuser.id); publicgroup.save(function(err, saved) { ... });
i expected results, aliceuser becoming fellow member of publicgroup:
{ "members": [ { "email": "alice@cc.com", "name": "alice", "id": 4, "createdat": "2014-06-23t22:20:01.967z", "updatedat": "2014-06-23t22:20:01.967z" } ], "name": "public", "createdat": "2014-06-23t22:19:57.489z", "updatedat": "2014-06-23t22:20:01.983z", "id": 1 }
now satisfactory, docu states save-method, not transactional, perchance leaving me user , grouping getting out of sync. docu continues:
because of this, recommended utilize equivalent model method instead.
to honest, i'm not hundred percent sure, mean, guessed sails team referring using group.update() method.
but if
publicgroup.members.add(aliceuser.id); /*same before*/ group.update(publicgroup.id, publicgroup, function(err, updated) { [...] });
aliceuser not appended fellow member of publicgroup:
{ "members": [], "name": "public", "createdat": "2014-06-23t22:19:57.489z", "updatedat": "2014-06-23t22:20:01.983z", "id": 1 }
hopefully, explains current problem in greater detail. if need farther information, please tell me so. lot time!
transactional database think , mean: - if have update in database more 1 table database-software automatically rollback if 1 part of update dont work.
thats useful bank accounts example. transfer money 1 bank business relationship have updates 2 items (update bank business relationship 1 set money = money -100 && update bank business relationship 2 set money = money +100). if 1 of updates failed, database rollback (so money dont disappear).
read more at: http://en.wikipedia.org/wiki/database_transaction
for waterline (the orm of sails) mean: if want create databaseupdates transactional have utilize query() or native()-methods.
many-to-many associations sails.js waterline
Comments
Post a Comment