Skip to content

Instantly share code, notes, and snippets.

@milosdakic
Created October 3, 2013 13:14
Show Gist options
  • Save milosdakic/6809627 to your computer and use it in GitHub Desktop.
Save milosdakic/6809627 to your computer and use it in GitHub Desktop.
sequelize associations issue
module.exports = function(State, Country) {
var data = require('../db/au.json');
// Create states
State.bulkCreate(data).done(function() {
// Find all newly added states where country associations is not set.
State.findAll({ where: { countryId: null }}).done(function(states) {
// Find the country we want to associate this with
Country.find({ where: { name: 'Australia' }}).success(function(australia) {
// Set country association
// Australia.hasStates(data).success(function(truthy) {
australia.setStates(states);
// });
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment