Tomasz Jezierski - Tefnet wrote:
>
> Awkward column_property is because of exception you'll get if try to set
> Column without column_proprerty around it:
> ---
> sqlalchemy.exc.ArgumentError: Column 'PhysObject.locationId' is not
> represented in mapper's table.  Use the `column_property()` function to
> force this column to be mapped as a read-only attribute.

If you keep the mapping straightforward and create attributes for each
column distinctly, all relationships can be correctly established using
documented patterns.  I don't really know what issues your approach is
having since I tend to focus on the working example, which is below.  
Adapt it to your custom metaclass one step at a time...though i feel
keeping the mappings explicit and declarative leads to better results.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class TefObject(Base):
    __tablename__ = "TefObject"
    Id = Column( Integer, primary_key=True,autoincrement=True)
    objectType = Column( String(128),nullable=False)
    __mapper_args__ = {'polymorphic_on': objectType,
'polymorphic_identity':"TefObject"}


class PhysObject(TefObject):
    __tablename__ = "PhysObject"
    phys_id = Column("Id", Integer,ForeignKey(TefObject.Id),
primary_key=True)
    barCode = Column( String(12),nullable=False)
    locationId = Column(Integer, ForeignKey('Location.Id', name =
'physobject_location_fkey', use_alter = True))
    location = relation("Location",
                        
primaryjoin="PhysObject.locationId==Location.location_id",
                        remote_side="Location.location_id"
                )
    __mapper_args__ = {'polymorphic_identity':"PhysObject",
"inherit_condition":phys_id==TefObject.Id}

class Location(PhysObject):
    __tablename__ = "Location"
    location_id = Column("Id", Integer,ForeignKey(PhysObject.phys_id),
primary_key=True)
    description = Column( String(128))

    __mapper_args__ = {'polymorphic_identity':"Location",
"inherit_condition":location_id==PhysObject.phys_id}


if __name__ == '__main__':

    engine = create_engine('sqlite://', echo=True)
    Base.metadata.create_all(engine)

    session = sessionmaker(engine)()

    room = Location(description = 'room 1', barCode = '1234')

    desk = PhysObject(barCode='555', location = room)

    session.add(room)
    session.add(desk)
    session.commit()

    for t in session.query(TefObject):
        print t, getattr(t, "location", None)


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to