Skip to content
EmileSpecs edited this page Nov 30, 2015 · 5 revisions

NB!

This fork is not bulletproof with minimal testing.

There is one caveat at this time: When using datasource.autoupdate in a boot script or wherever and there is data in your database you might get "foreign key constraint fails" errors.

This happens because the order in which the tables are updated is important, since a table that belongsTo another can't update before its parent does (or at least this seems like the logical reason there would be errors).

You'll notice that launching the app multiple times will result in it eventually succeeding, since the updates are asynchronous and might actually resolve in the correct order.

To make sure the errors don't happen or are minimal, my approach is to use two nested datasource.autoupdate calls:

datasource.autoupdate(updatePrimaryModels, function(err) {
  if (err) throw err;
  datasource.autoupdate(updateSecodaryModels, function(err) {
    if (err) throw err;
  });
});

The primary models array contains the model names of the models that the secondary models belongs to. This way, updates are forced to occur in an order that prevents errors.

Clone this wiki locally