balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Recursive populate/create of nested associations #6065

Closed EyalSi closed 5 years ago

EyalSi commented 10 years ago

I'm trying the following code to populate the database. Seems like on the third level of data (categories2), I'm getting the below error.

Any idea how can I import the data to the db - recursively? Is it a bug?

/* Category.js */
module.exports = {
    autoPK: false,
    attributes: {
            id: { 
                type: 'string',
                primaryKey: true
            },
            items: {
                collection: 'item'
            }
    }
};

/* Item.js */
module.exports = {
    attributes: {
        name: {
            type: 'string',
            required: true
        },
        selections: {
            collection: 'category'
        }
    }
};

/* bootstrap.js */
var categories1 = [
    {
        "id": "salads_1",
        "name": "Salads",
        "order": "1",
        "items": [{
                    "name": "Vegi Salad 1",
                    "price": "53"
        }]
    }];

var categories2 = [
    {
        "id": "salads_2",
        "name": "Salads",
        "order": "1",
        "items": [{
                    "name": "Vegi Salad 2",
                    "price": "53",
                    "selections": [{
                         "id": "dressings",
                         "name": "dressings",
                         "items:": [{
                                "name": "Vinaigrette"
                         },{
                                "name": "Thousands islands"
                         }]
                    }]
        }]
    }];

    Category.create(categories1).exec(function(err,categories){
         sails.log.warn(err);
    });

    Category.create(categories2).exec(function(err,categories){
         sails.log.warn(err);
    });

This is the error I'm getting: Error (E_UNKNOWN) :: Encountered an unexpected error Details: [ { type: 'insert', collection: 'item', values: { name: 'Vegi Salad 2', price: 53, selections: [Object], createdAt: Mon Jun 09 2014 13:01:05 GMT+0000 (UTC), updatedAt: Mon Jun 09 2014 13:01:05 GMT+0000 (UTC) }, err: undefined } ]

CWyrtzen commented 10 years ago

@EyalSi did you confirm this as a bug? Have you tried asking in another forum? If you found a solution, link us up with your answer or, if someone confirmed this as a bug we can mark it as such.

Sails v0.10 is finally here: Upgrade | Contribution Guide | Stackoverflow | Google Group | IRC | Build Status

CWyrtzen commented 10 years ago

Is this a bug? @uncletammy @loicsaintroch

uncletammy commented 10 years ago

@EyalSi, it looks like you're attempting a Many-To-Many association but you forgot the via key in your model configurations. You'll also need the dominate key set on at least one of the models so Waterline knows how to build the join table.

See Many-To-Many Association Docs for more info

For future reference, you're likely to get a quicker response if you post the version of Sails and Waterline you are using as well as the adapter name and version.

CWyrtzen commented 10 years ago

The answer is above and can be found in search results. I'm going to close this up. Thanks all~