geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

Removing association from hasOne/belongs #154

Open xreader opened 10 years ago

xreader commented 10 years ago

I have two models that are associated with hasOne / belngsto relation

In documentation is described the "set" and "get" API methods but how can one remove item from associaton or is it only possible to override it with another item?

Here is one test that fails

, 'test add genus to name': function (next) {
        var name = Name.create({name: "mepecus"});
        name.save(function (err, data) {
            assert.equal(err, null);
            var genus = Name.create({name: "pepecus"});
            name.setGenera(genus);
            name.save(function (err, name) {
                assert.equal(err, null);
                name.getGenera(function (err, data) {
                    //removing association
                    Name.remove({generaNameId:data.id}, function (err){
                        assert.equal(err, null);
                        name.getGenera(function (err, data){
                            assert.equal(err, null);
                            assert.equal(data, null);
                        });
                    });
                });
            });
        });
    }
ben-ng commented 10 years ago

There is no api for removing associations right now, you have to find the associated model's id and delete it.

xreader commented 10 years ago

ok, but It is not possible in my case because the model from association belongs to another items

mde commented 10 years ago

Actually there is an API for removing associations, which I snuck into the release branch a few weeks ago: https://github.com/mde/model#removing-associations

Let me know if it works okay for you. :)

xreader commented 10 years ago

@mde Hi, i updated model to 0.4.15 but i still get exception TypeError: Object {..} has no method 'removeParent'


var Name = function () {
...
    this.hasOne('Generic');
}

var Generic = function () {
...
    this.belongsTo('Name');
}

 , 'test remove association to genus from name': function (next) {
        var name = geddy.model.Name.create({name: "demecus"});
        name.save(function (err, data) {
            var parent = geddy.model.Name.create({name: "Tree"});
            name.setParent(parent);
            name.save(function (err, data) {
             name.removeParent(parent, function (err) {
                     next();
                });
            });
        });
    }
mde commented 10 years ago

The model names in the test don't seem to be the same as the definitions. You have two instances of Name, where one is trying to set the 'parent' association on the other. From the definitions, it looks like you should have a Name and a Generic, and have the Name instance do setGeneric on the Generic instance.

We do have a few basic tests for the remove API, and they are passing: https://github.com/mde/model/blob/master/test/integration/adapters/shared.js#L928

ben-ng commented 10 years ago

Oh wow, a surprise feature :D