[storm] Validation of referenced objects

Jamu Kakar jkakar at kakar.ca
Thu Aug 18 12:00:20 UTC 2011


Hi Michael,

On Thu, Aug 18, 2011 at 1:25 PM, Michael Löffler <ml at mldesign.net> wrote:
> I'm looking for an elegant way, to validate referenced objects. Let's say, I
> have a table 'persons' with the attributes 'father', 'mother', 'age', and I
> want to validate for example, that the parents are actually older then their
> children. So I have code like
>
> class Person(object):
>    __storm_table__ = 'persons'
>    id = Int(primary=True)
>    father_id = Int()
>    father = Reference(id, father_id)
>    ...
>
> Adding a validator to the father_id attribute would have to look up the
> object by the id first, and then process the validation, even if the
> reference already knows the referenced object. This extra lookup would be
> inefficient in my eyes. On the other side, a Reference doesn't seem to have
> a validation mechanism. Would it be wise, to somehow subclass Reference, or
> do I better stick with the lookup of the id? Are there any side effects I
> should be aware of?

There's no great way to do this in Storm, right now.  You could define
a validator for the father_id property, as you suggest, but one
concern is that creating many Person instances will run a lot of
individual queries to fetch the references.  If you only create one at
a time, it's not a big deal, but if you create many instances you
probably want to write a function create_people(...) that can fetch
the related objects using a single query and perform the validation in
a batch.

Thanks,
J.



More information about the storm mailing list