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 doesn't work beyond a depth of 2 with (inversed) hasOne relationships #340

Open
entropitor opened this issue Sep 18, 2013 · 7 comments

Comments

@entropitor
Copy link
Contributor

My data-structure looks like this:

models.Team.hasOne('match',models.Match,{reverse: "teams"});
models.Player.hasOne('team',models.Team, {reverse: "players"});

now when I do

req.models.Match.get(id,{cache: false, autoFetchLimit: 2, autoFetch: true},function(error, match) {res.jsonp(match);});

I don't get my players into the match object :(

@entropitor
Copy link
Contributor Author

I'm not sure if I should make a new issue for this, but I'm also noticing that autoFetch doesn't use JOIN queries to get its data, is there a reason for this?

@dresende
Copy link
Owner

autoFetch is just a shortcut for .find()+.get*(). I don't know if it could be changed to take advantage of JOINs.

About the bug, I think I saw a line that could be doing this, but I'm not sure, I need to investigate.

@idekterev
Copy link

I have also observed random behaviour using autoFetch: I have exactly the same set-up in terms of inverse associations described by caske33.
In some parts of my code the autoFetch works, and yet exactly the same code elsewhere sometimes fails to return any associations, and sometimes succeeds with 1 level, sometimes with 2 levels. It is truly 'random' as far as I can tell.
I had to stop using autoFetch, and do chained lookups - it seemed like the only way to fix the problem.

@dresende
Copy link
Owner

DIsable cache and your code will probably go away (see #241, #339).

// before connecting..
orm.settings.set("instance.cache", false);

@entropitor
Copy link
Contributor Author

This still doesn't work btw. Nor with the cache disabled globally nor locally (like I did in the initial post).

@toddpi314
Copy link

FYI, disabling cache and turning all models into autoFetch (at the schema level) fixed for me.

The findChain syntax is ideal, where we can disable cache and force a deep auto-fetch on a query-by-query basis.

The current situation is disabling cache at a global level really slams the DB connection.

@toddpi314
Copy link

Is this a problem with the autoFetch state relative to caller and the cache?

Historically, when something like this comes up in an ORM, it is because in some scenarios an entity has a state which may or may not contain deep fetch elements based on the context/behavior of the caller. To avoid this, you may consider extending your cache key fields to contain meta-data relative to the auto-fetch history.

For instance, if entity is stored by "id" and keyed for hit/miss, you can extend the key to "id_propertyA_propertyB" to identify that the instance is associated to an auto fetch with those fields.

Hope this helps.

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

No branches or pull requests

4 participants