ufront / ufront-orm

The Object Relational Mapper, allows easy, type-safe access to your database. Builds on Haxe's SPOD but adds macro powered relationships, validation and client side usage
MIT License
13 stars 4 forks source link

order of save command influences record creation for manytomany #8

Closed milkmangames closed 9 years ago

milkmangames commented 9 years ago

Assume that you have db.Object's Order and Product. An Order has many Products and Product has many Orders using ManyToMany.

Imagine you are populating this data for the first time:

var someProducts=new Array();
for (p in 0...5)
{
    var product=new Product("productName_"+p);
    product.save();
    someProducts.push(product);
}

for(o in 0...10)
{
    var order=new Order();
    order.products.add( someProuducts[randomFromTo(0,someProducts.length)]);
    order.save();
}

The resulting join table records will be incorrect; the order field will always be 0 and in ORM each product's orders property will have a length of 0.

This is because order.save() was not called until after order.products.add(). At the time of .add(), the haXe object existed, but the database had not be written to and therefore the order did not have an actual ID value.

The least complex solution would be to throw an exception when trying to manipulate the contents of a ManyToMany property if the Object is not yet saved.

Of course, it would be better if it just worked (logic would be complex though.)