On Nov 25, 2011, at 6:05 PM, espresso maker wrote:

> Hi,
> 
> I am wondering what's the best practice to implement relationship
> constraints in sqlalchemy.
> 
> Example:
> 
>    Let's assume we have  Account (1) <---- (n)SNMPMonitor  and we
> want to have the following constraints:
> 
>        No account can have more than 10 SNMP Monitors.
>        No account can have more than 5 SNMP Monitors if their
> subscription level is X
>        No account should have more than 7 SNMP Monitors started  at
> the same time.
> 
> SNMPMonitor has methods like : start/stop/reload
> 
> I am wondering where is it best to implement these constraints?
> 
> I am trying to avoid having methods like start_monitor in Account so
> that Account can check if this action is allowed or not. Having such a
> method means I have to pass the monitor instance to start_monitor
> which will eventually do monitor.start(). I feel this is dirty because
> 'start_monitor' should belong to 'SNMPMonitor' as a method named
> 'start'. Also having a method like 'add_monitor' in Account defeats
> having a backref like a.monitors that allows me to do
> a.monitors.append(new_monitor).

the most straightforward route would be the @validates decorator or an 
attribute event.

Following that, validation can also be done in the before_flush() event though 
@validates is probably plenty for this.

That's of course assuming app-side validation is "enough" here, taking it to 
the DB side becomes a bigger deal (triggers, SPs).

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to