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

Olaf Conradi olaf at conradi.org
Sun Aug 17 20:03:32 BST 2008


Hi Jamu,

I've been experimenting with your approach. There is just one thing I
can't get to work without explicitly flushing the store.

class Subject(Storm):
    __storm_table__ = 'subjects'
    id = Int(primary=True)
    name = Unicode(allow_none=False)
    type = Int(allow_none=False)
    type_id = Int()

    _info = None

    def __init__(self, subject_class, name, **kwargs):
        if subject_class not in subject_types.values():
            raise UnregisteredSubjectError('%s is not a registered subject '
                                           'class' % subject_class)
        store = global_zstorm.get(config['storm.database.persons.name'],
                                  config['storm.database.persons.uri'])
        self.name = name
        self.type = subject_class.type
        store.add(self)
        self._info = subject_class(self, **kwargs)

    @property
    def info(self):
        if self._info is not None:
            return self._info
        assert self.id is not None
        subject_class = subject_types[self.type]
        if not hasattr(subject_class, '__storm_table__'):
            info = subject_class.__new__(subject_class)
            info.subject = self
        else:
            info = Store.of(self).get(subject_class, self.id)
        self._info = info
        return info


class Agent(Storm):
    __storm_table__ = 'agents'
    id = Int(primary=True)
    subject_id = Int()

    subject = Reference(subject_id, 'Subject.id')

    def __init__(self, subject):
        store = Store.of(subject)
        self.subject = subject
        store.add(self)
        store.flush()
        self.subject.type_id = self.id


class Person(Storm):
    __storm_table__ = 'persons'
    id = Int(primary=True)
    subject_id = Int()
    gender = Enum(map={'Undisclosed': 0, 'Male': 1, 'Female': 2})
    date_of_birth = Date()

    subject = Reference(subject_id, 'Subject.id')

    def __init__(self, subject):
        store = Store.of(subject)
        self.subject = subject
        store.add(self)
        store.flush()
        self.subject.type_id = self.id


The following code is in the controller:

        p = Subject(Person, name=u'hi there')
        p.info.gender = self.form_result['gender']


When I remove the explicit flushes in __init__ of both Person and
Agent, the type_id is not written in the subjects table.
Is there a way to set type_id of subject without flushing?

Thanks,
 -Olaf



More information about the storm mailing list