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

Creating model with autoFetched association hasMany #242

Closed
gaetsd opened this issue Jul 25, 2013 · 6 comments
Closed

Creating model with autoFetched association hasMany #242

gaetsd opened this issue Jul 25, 2013 · 6 comments

Comments

@gaetsd
Copy link

gaetsd commented Jul 25, 2013

Hi, I write this little test to explain my issue:


var orm = require('orm');
orm.connect("mysql://[email protected]/test?debug=true", function (err, db){

    console.log("CONNECTED TO DATABASE");

    ///////////////////////
    // CREATE THE PARENT
    ///////////////////////

    var Person = db.define('person', {
        name : String
    });

    ///////////////////////
    // CREATE THE CHILD
    ///////////////////////

    var Animal = db.define('animal', {
        name : String
    });

    ///////////////////////
    // LINK THEM UP
    ///////////////////////

    Person.hasMany("pets", Animal,{}, {
        autoFetch: true
    });

    ///////////////////////
    // Sync
    ///////////////////////

    db.sync(function(){

        console.log("SYNC'D DB `test`");

        ///////////////////////
        // Test insert
        ///////////////////////

        var John = new Person();
        John.name = "John";


        John.save(function(err,obj){
            if(err)
                console.log("err: " + err);
            console.log(obj);
            
            console.log("END");
        });

    });

});

When I execute this code, I have the error:


No associations defined

But when I remove the autoFetch option, it working correctly. Can you help me, please.

@dresende
Copy link
Owner

The error happens when you call John.save()?

@starwing
Copy link

I have meet this issue, too :-(

after remove autoFetch option, the error is gone.

btw, there are two issue related with this:

  • if I define autoFetch and reverse option, the reversed table's query will load this table. e.g. group has many user, reversed user has group, if this is autoFetched, if I get a group, there is a array in group object that the users of group, but when I get a user, there also is a array in user object! how can I get "one-side" direction of autoFetch?
  • when I use .count to getXXXX of hasMany, the generated SQL is wrong :-( e.g. if I write this:
group.getMember({id: req.user.id}).count(function(err, result) { ... })

instead of this SQL is executed:

SELECT count(*) as c FROM user t1 JOIN group_member t2 ON t2.user_id = t1.id AND t2.group_id = 1;

but just:

SELECT count(*) as c FROM user WHERE id=1;

is that right behavior?

@gaetsd
Copy link
Author

gaetsd commented Jul 26, 2013

Yes the error happens when I call John.save(). There is the same issue when I use the create method like this:


Person.create({
    name:'John'
}, 
function(err, obj){
    if(err)
        console.log("err: " + err);
    console.log(obj);
            
    console.log("END");
});

The error appears when Person.create() is called

@ctaepper
Copy link

i can confirm this... this bug seems to come out of nowhere as it was working earlier...

@dresende
Copy link
Owner

Please try this latest commit.

@gaetsd
Copy link
Author

gaetsd commented Jul 30, 2013

This last version works fine. I've tried it with my test and in my application and it's working. Thanks a lot.

@gaetsd gaetsd closed this as completed Jul 30, 2013
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