Next / Previous / Contents / NM Tech homepage

6.13. Object-relational mapping

This section of the pycbc.py file describes the mapping between objects and database tables, and the relations between the tables.

A number of new attributes are added to the mapped classes in this section:

TableAttributeFunction
nations .regions Iterates over the Region instances for this nation.
regions .nation The related Nation instance for this region.
.circles Iterates over the Circle instances that are all or partly within this region.
.cir_regs Iterates over the CirReg instances that are related to this region. This attribute is necessary because the CirReg instance has a column reg_pos that is not found in either the circles or region table.
cir_reg .region The related Region instance for this row.
.circle The related Circle instance for this row.
physios .circles Iterates over the circles that are related to this stratum.
.cir_physios Iterates over the CirPhysio instances for this stratum.
cir_physio .circle The related Circle.
.physio The related Physio.
circles .regions Iterates over the Region instances for this circle.
.cir_regs Iterates over the CirReg instances for this circle.
.physios Iterates over the Physio instances for this circle.
.cir_physios Iterates over the CirPhysio instances for this circle.
.efforts Iterates over the Effort instances for this circle.

The first table to be mapped is the nations table, which has a one-to-many relation with the regions table.

pycbc.py
#================================================================
# Mapper configuration
#----------------------------------------------------------------
    orm.mapper(Nation, nations_table,
        properties={
            'regions': orm.relation(Region, backref='nation')})

The regions table has a many-to-many relation to the circles table, through the secondary table cir_reg.

pycbc.py
    orm.mapper(Region, regions_table,
        properties={
            'cir_regs': orm.relation(CirReg, backref='region'),
            'circles':  orm.relation(Circle,
                secondary=cir_reg_table, backref='regions')})
    orm.mapper(CirReg, cir_reg_table)

The physios table has a many-to-many relation with the circles table, through the secondary table cir_physio.

pycbc.py
    orm.mapper(Physio, physios_table,
        properties={
            'circles':     orm.relation(Circle,
                secondary=cir_physio_table, backref='physios'),
            'cir_physios': orm.relation(CirPhysio, backref='physio')})
    orm.mapper(CirPhysio, cir_physio_table)

The circles table has a one-to-many relation with the efforts table. (Its two many-to-many relations were set up above.)

pycbc.py
    orm.mapper(Circle, circles_table,
        properties={
            'cir_regs':     orm.relation(CirReg, backref='circle'),
            'cir_physios':  orm.relation(CirPhysio, backref='circle'),
            'efforts':      orm.relation(Effort, backref='circle')})

The one-to-many relation between the effort table and the censuses table is mapped here.

pycbc.py
    orm.mapper(Effort, efforts_table,
        properties={
            'censuses': orm.relation(Census, backref='effort')})
    orm.mapper(Census, censuses_table)