[Nitro] og: automatic deletion of records in M:N relations?

Jonathan Buch john at oxyliquit.de
Wed Aug 23 09:28:02 EDT 2006


> Try to set the relation only to other of the classes (it's two-way
> automatically).

> so Tags get deleted if the Item is deleted, I don't know why you would
> want to remove the Items if a Tag is removed...

Well, with Tags you're quite right, but you may think of a online shop,  
where you have a `ShoppingCart` and `ShoppingCartItem`'s.  When the User  
shops and suddenly logs out, the easiest thing to do is just to delete the  
shopping cart, which will then 'cascade' and delete the other items as  

We have two different cases now, this means that this should be  
configurable.  I think this is a classic case of constraints like used in  
PostgreSQL and any other serious DB.

Right now I have this idea in my head that Og could utilize information  
 from the DB or simulate when the DB doesn't support constraints.

An example:

class OgModel
   has_one :item, Item, :on_delete => 'cascade'

where the settings are:

* `:on_delete`
* `:on_update`

and possible values are:

* 'cascade' (update/delete all referenced items),
* 'restrict' (don't do anything to referenced items),
* 'set null' (set the property on the referenced item to 'NULL'),
* 'set default' (reset the property of referenced item to it's 'DEFAULT'  

Not sure how this could be implemented (from th Og side).  Something with  
callbacks on `:on_delete` which I think is already there plus some  
book-keeping within the OgModel...



Uh, that lead me to a similar thought.  PostgreSQL also has other  
constraints like 'UNIQUE', 'CHECK (price > discounted_price)'.  Maybe the  
current validation system can be intercepted at some point and create the  
same constraints in the DB too if possible.

Then you could write

class OgModel
   property :price, Integer
   validate_unique :price

instead of

class OgModel
   property :price, Integer, :unique => true
   validate_unique :price

This could then also be used to _assume_ validity.  When the DB is able to  
validate 'UNIQUE' at input/update time (and throw errors if failed) then  
the validator could just go ahaid and assume that the validity of that  
particular constraint is fullfilled.  This would lessen the overhead of  
valdation due to fewer roundtrips to DB.


Maybe another recognized option for `property`:

class OgModel
   property :price, Integer, :constraint => 'check(...)'

Right now I would use `:sql =>` for that, but what is when I use the  
VarChar(20) option?  The `:sql` from it would get overridden....  Or use  
the undocumented `:extra_sql`?

But what about non-sql stores...

That thought isn't finished, so don't mind me, I find it useful to write  
while developing my thoughts ;)


I haven't found the constraint code for postgres in 0.40... did I overlook  
it or is it being reworked, revoked or otherwise?  It was quite some  
hackish code and didn't work well with Polymorphic stuff, I liked it  

Feel the love

More information about the Nitro-general mailing list