dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 377 forks source link

hasMany mergeTable field updating error #519

Open mike-zenith opened 10 years ago

mike-zenith commented 10 years ago

I need confirmation on this, i do not know if its only reproducable in my system or it really is a bug.

The 'extra' fields in hasMany merge table are populated to the main model but cannot be set through it.

TypeError: Cannot read property 'key' of undefined
var User = db.define('user', {  
       name: { type:'string', size: 32 }
});

var Points = db.define('points', {
       name: { type: 'string', size: 16 }
});

User.hasMany(
       'points',
       Points, 
       {  value: { type: 'integer', size: 4, defaultValue: 0 } }, 
       { reverse: true, key: true });

User.create({name: 'peter'}, function (e, Peter) {
       Points.create({name: 'strength'}, function (e, Str) {
              Peter.addPoints(Str, { value: 1 });
       });
});

// after inserting some dummy record
User(1).getPoints(function (pts) {
       var P = pts[0];
       // getter is fine
       console.log(P.value);
       // error 
       P.value = P.value + 1;

       // using the 'extra' field instead of the populated setter
       // works fine
       P.extra.value = P.extra.value + 1;
});
dxg commented 10 years ago

Not really sure why extra properties are available directly on the instance (as you could easily have property clashes) however if instead of P.value = you do P.extra.value = then it will work.