[storm] subclassing and foreign keys with respect to properties from parent

Olaf Conradi olaf at conradi.org
Sat Aug 23 10:54:54 BST 2008


Hi Jamu,

2008/8/22 Jamu Kakar <jkakar at kakar.ca>:
> I'm not sure exactly why your code doesn't work.  I've written a
> doctest to describe this pattern and to provide working code
> examples.  I'll be pushing this in a branch to get it included along
> with other Storm doctests.  Here it is (it works):

Thanks for writing this. This helped me make my implementation cleaner.
I like the StoredPersonInfo classes to factor out the common part of
binding Info classes to the Person class.

The mistake I made in my previous implementation was that I tried to
keep an id in the person table to the info classes table.
But because of the info property it is not needed and every info class
already stored a foreign key to the person class. So it was redundant
anyway.

It's working fine now. Thanks.

> The infoheritance pattern uses composition instead of inheritance to work
> around the multiple table limitation.  A base Storm class is used to
> represent
> all objects in the hierarchy.  Each instance of this base class has an info
> property that yields an instance of a specific info class.  An info class
> provides the additional data and behaviour you'd normally implement in a
> subclass.  Following is the design from above converted to use the pattern.
>
> ...
> ...     @property
> ...     def info(self):
> ...         if self._info is not None:
> ...             return self._info
> ...         assert self.id is not None
> ...         info_class = person_info_types[self.info_type]
> ...         if not hasattr(info_class, "__storm_table__"):
> ...             info = info_class.__new__(info_class)
> ...             info.person = self
> ...         else:
> ...             info = Store.of(self).get(info_class, self.id)

I am using seperate id's for each info_class

            info = Store.of(self).find(info_class,
                                       info_class.person_id == self.id
                                      ).one()

> ...         self._info = info
> ...         return info
>
>
>>>> class PersonInfo(object):
>
> ...
> ...     def __init__(self, person):
> ...         self.person = person
>
>
>>>> class StoredPersonInfo(PersonInfo):
>

A new id for every info class

    id = Int(allow_none=False, primary=True)

> ...     person_id = Int(allow_none=False, primary=True)

    person_id = Int(allow_none=False)

> ...     person = Reference(person_id, Person.id)
>



More information about the storm mailing list