Skip to content

Creating model with autoFetched association hasMany #242

Closed

Description

@gaetsd

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


var orm = require('orm');
orm.connect("mysql://root@127.0.0.1/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.

Activity

  1. dresende commented on Jul 25, 2013

    @dresende
    Owner

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

  2. starwing commented on Jul 26, 2013

    @starwing

    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?

  3. gaetsd commented on Jul 26, 2013

    @gaetsd
    Author

    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

  4. ctaepper commented on Jul 26, 2013

    @ctaepper

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

  5. dresende commented on Jul 29, 2013

    @dresende
    Owner

    Please try this latest commit.

  6. gaetsd commented on Jul 30, 2013

    @gaetsd
    Author

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

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions