[okfn-help] vdm: contuity relation in self-referential tables

Martijn Faassen faassen at startifact.com
Thu Aug 26 17:48:34 BST 2010


Hi there,

With vdm for each table -> class mapping you also create a
revision_table -> revision_class mapping.

create_object_version() is what constructs both the revision class as
well as the mapping to it. This sets up a 'continuity' relation.

Things broke down however when I created a self-referential table.
SQLAlchemy (for some reason) requires an additional primaryjoin clause
on any relation connected to the self-referential table.

But since create_object_version creates such a relation automatically
(continuity), SQLAlchemy couldn't figure out anymore how that was
supposed to work.

This is what sets up the continuity:

        'continuity':relation(base_object,
            backref=backref('all_revisions_unordered',
                cascade='all, delete, delete-orphan'),
                order_by=rev_table.c.revision_id.desc()

I hacked it up to add the following:

    # XXX this can break if there are more than one tables
    from sqlalchemy.orm import class_mapper
    continuity_table = class_mapper(base_object).tables[0]

and then later...

        'continuity':relation(base_object,
                              primaryjoin=(rev_table.c.continuity_id ==
                                           continuity_table.c.id),
            backref=backref('all_revisions_unordered',
                cascade='all, delete, delete-orphan'),
                order_by=rev_table.c.revision_id.desc()
            ),

i.e. I added what I hope is the correct primaryjoin expression. When I
run the vdm tests that doesn't seem to create breakage, and it seems
to fix the self-referential story. Though I must say the test coverage
for 'continuity' isn't very broad..

Thoughts?

Regards,

Martijn



More information about the okfn-help mailing list