Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofetch and subqueries (or nested objects) #490

Open
tb01923 opened this issue Apr 20, 2014 · 2 comments
Open

Autofetch and subqueries (or nested objects) #490

tb01923 opened this issue Apr 20, 2014 · 2 comments

Comments

@tb01923
Copy link

tb01923 commented Apr 20, 2014

I have a page which bootstraps itself with a list of 25 objects prior to making a REST request for the entire list (234 objects in test).

Because I access the same query through two different entry points, I abstract into a function:

ideaListTrailingWeek: function index(req, limit){
    var user = req.user
    var Idea = req.db.models.dili_forum;

    var now = new Date()
    // set now to be 100 days ago because my data set is old
    now = dateHelper.addDays(now, -100)
    var weekAgo = dateHelper.addDays(now, -7)

    var promise = _.isUndefined(limit) ?
        Idea.qFind({ date_added: orm.gte(weekAgo) },{autoFetchLimit: 2}, ['-id']) :
        Idea.qFind({ date_added: orm.gte(weekAgo) },{autoFetchLimit: 2}, limit, ['-id'])

    return promise.then(function (ideas) {

        var ideasFinal = _.chain(ideas)
            .map(ideaTx.lightIdea)
            .value()

        return ideasFinal
    })
}

This is all working OK (picking the right promise and executing it). The model is

 Idea -many-> IdeaIndustry -one-> Industry
 Idea -one-> User -one-> Member

If I access with the call that bootstraps the page with the "limit"" first, the page loads fine. The Model is loaded to the proper depth. However the subsequent rest call comes through (picking the promise without the limit), and doesn't return any of the nested objects (not the first level User, nor the deeper objects).

If I reset the Node server and access through the REST call (picking the promise without the limit), the entire data set is loaded with proper depth.

I have set all my relationships up with the hasOne relationship, out of comfort of having used it that way in previous projects. That said, I plan to investigate the many relationship once I have this working properly. This project has a legacy DB built by offshore contractors with poor names, and mis-labeled fields. Models:

   var Idea = db.define('dili_forum', {
        id : Number,
        title : String,
        content : String,
        user_id : Number,
    }, {
        autoFetch: true
    }) ;

   var IdeaIndustry = db.define('teaser_industries', {
        id: Number,
        teaser_id: Number,
        sub_industry_id : Number
    }, {
        autoFetch: true
    }) ;

    var Industry = db.define('mas_flattened_industry', {
        industry_id: Number,
        industry_name : String,
        parent_industry_id: String,
        parent_industry_name: String,
        category_name: String
    }, {
        id: 'industry_id',
        autoFetch: true
    }) ;

I can post the user model chain if that will help. Here is how the relationships are set up:

    Idea.hasOne('user', User, {
        reverse: 'ideas'
        , field: 'user_id'
        , autoFetch: true
    })

    IdeaTicker.hasOne('idea', Idea, {
        reverse: 'tickers'
        , field: 'teaser_id'
        , autoFetch: true
    })

    IdeaIndustry.hasOne('idea', Idea, {
        reverse: 'industries'
        , field: 'teaser_id'
        , autoFetch: true
    })

    IdeaIndustry.hasOne('industry', Industry, {
        field: 'sub_industry_id'
        , autoFetch: true
    })

Any thoughts?

@dxg
Copy link
Collaborator

dxg commented Apr 21, 2014

Does setting cache to false help at all?

@twk-b
Copy link

twk-b commented Sep 22, 2014

I have the same problem. Using orm with passport and passport-local, when the user logs in initially the account is retrieve correctly (user ->hasmany user_group (joined with user_user_group), user ->hasMany maillist (joined with maillist_user), the user_id is stored in the session.

When the user comes back (very next request) the same function is used to load the user but this time both user_group and maillist = undefined.

There is something about the order of operations/timing. It seems like maybe whichever call happens first is successful (maybe). Sometimes both work.

Setting { cache: false } on the query seems to have improved it (maybe?), but not stopped it completely.

This happens using both .find() and .get() methods.

I had been running orm 2.1.13 for a long time (2.1.14 made changes to keys/id required model fixes), but last night I went through the process and fixed my models (removing key: true, and leaving just { id: key_name }, but the problem still exists on 2.1.19

I then wrote a small program node program that tried to do the query using both find/get and ran that 50 times in a shell loop, it always succeeded (getting the right number of groups). I then updated the program so that instead of looping in a shell, it looped inside the program. This also seemed to work without issue.

However in the node/express environment it will work for one request and fail for the next. Once it does fail the only way to restore it to working seems to be restarting the server (I tried deleting the session to see if that happened, I tried flushall on redis also to see if there was another way to clear the issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants