bredikhin / barrels

Simple DB Fixtures for Sails.js
MIT License
85 stars 34 forks source link

Manual associations don't work as expected #17

Closed caasjj closed 10 years ago

caasjj commented 10 years ago

Manually associating models seems not to be possible to do reliably. In the following example, different passports get associated to users on multiple startups of the server.

I have two models I'm trying to populate. One is a User model, the other a Passport model, where a User has many Passports. So, I try to initialize them as follows:

User:

[
    {
        "username"     :"admin",
        "email"        :"admin@admin.com",
        "admin"        :true,
        "confirmed"    :true
    },
    {
        "username"     :"demo",
        "email"        :"demo@demo.com",
        "admin"        :false,
        "membership"   :"member",
        "confirmed"    :true
    },
    {
        "username"     :"user",
        "email"        :"user@user.com",
        "admin"        :false,
        "membership"   :"member",
        "confirmed"    :true
    }
]

and Passport:

[
    {
        "protocol":"local",
        "password":"adminadmin",
        "user": 1
    },
    {
        "protocol":"local",
        "password":"demodemo",
        "user":2
    },
    {
        "protocol":"local",
        "password":"useruser",
        "user":3
    }
]

The code to populate the database is as follows:

var _ = require('lodash');
var Barrels = require('barrels');
var barrels = new Barrels();
var fixtures = _.keys(barrels.data);
barrels.populate(fixtures, next, false); //<== passing false here

I can see in the database that sometimes 'admin' user gets 'demo' passport, and in fact i can login to 'admin' account with 'demodemo' password.

I'm disabling auto-association because I have models with required:true on associations, and this causes barrels to fail to load models.

bredikhin commented 10 years ago

Did you check out https://github.com/bredikhin/barrels/issues/12#issuecomment-60329812?

caasjj commented 10 years ago

Thanks for the follow up, and though I did look through the list of existing issues, I completely missed that exchange - sorry, my bad.