Dear list,

I'm still working on a DNS administration web interface and have to deal
with DNS records. This is what I have so far:

-------------------------------------------------------------------------------
import sqlalchemy as sql
import sqlalchemy.orm as orm
from sqlalchemy.databases.postgres import PGInet

records_table = sql.Table(
    'records', metadata,
    sql.Column('id', sql.Integer, primary_key=True),
    sql.Column('name', sql.Unicode(80)),
    sql.Column('type', sql.Unicode(10)),
    sql.Column('content', sql.Unicode(200)),
    sql.Column('inet', PGInet),
)

class Record(MyOrm):
    @property
    def ptr_records(self):
        """
        Returns matching PTR records for an A record
        """
        assert self.type=='A'
        assert self.inet!=None
        return Record.q().filter_by(type='PTR', inet=self.inet)

orm.mapper(Record, records_table)
-------------------------------------------------------------------------------

So for an address (A) entry I'd like to find out if there is a matching
PTR record. Match criteria are the "inet" column. So if I have a certain
A record...

    a = query(Record).filter_by(type='A').one()

...and like to find out the matching PTR records I would call...

    ptr = a.ptr_records

This works okay so far. But somehow it feels wrong to do queries in
properties I add to the Record class. Especially since the ptr_records
do not get cached and the query is run every time I access this
property. So I wondered how to do that as properties of the mapper. I
started with
http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_relation_customjoin

But that example deals with User and Address tables and not with
self-references. I suspect I have to alias the table. Roughly I'm
thinking of something like:

    properties={
        'ptr_records': relation(Record, primaryjoin=and_(
            records_table.c.type=='PTR', 
records_table.c.inet=records_table2.c.inet
            ))
    }
 
I don't know how to say "match other Record objects where the 'inet'
column contains the same value". How do I do that correctly?

Cheers
 Christoph

P.S.: I simplified the models for this posting so bear with me if this
      is not code that would run as is.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to