Dnia 2009-08-27, czw o godzinie 10:51 -0400, Michael Bayer pisze: > you can also set up primaryjoin using the actual table > columns (i.e. PhysObject.locationId == Location.__table__.c.id). >
Thanks, that worked really well :). Now i am stuck with something else in this subject: ===================================================================== # available at http://filip.math.uni.lodz.pl/column_pairs.py # Fails with Python-2.5.4 and SQLAlchemy-0.5.5 import sqlalchemy import sqlalchemy.ext.declarative ''' Fails with: File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/orm/properties.py", line 836, in _determine_synchronize_pairs "marked as viewonly=True." % (self.primaryjoin, self) sqlalchemy.exc.ArgumentError: Could not locate any equated, locally mapped column pairs for primaryjoin condition '"PhysObject"."locationId" = "Location"."Id"' on relation PhysObject.location. For more relaxed rules on join conditions, the relation may be marked as viewonly=True. ''' class TefDeclarativeMeta(sqlalchemy.ext.declarative.DeclarativeMeta): def __init__(cls, classname, bases, dict_): if '_decl_class_registry' in cls.__dict__: return type.__init__(cls, classname, bases, dict_) base = bases[0] cls.__tablename__ = cls.__name__ if hasattr(base, 'Id'): cls.__mapper_args__ = {'inherit_condition': cls.Id == base.Id, 'polymorphic_identity': cls.__name__} sqlalchemy.ext.declarative._as_declarative(cls, classname, dict_) return type.__init__(cls, classname, bases, dict_) Base = sqlalchemy.ext.declarative.declarative_base(metaclass=TefDeclarativeMeta, mapper=sqlalchemy.orm.mapper) class TefObject(Base): Id = sqlalchemy.Column( sqlalchemy.types.Integer, primary_key=True, autoincrement=True) objectType = sqlalchemy.Column( sqlalchemy.types.String(128), nullable=False) __mapper_args__ = {'polymorphic_on': objectType} class PhysObject(TefObject): Id = sqlalchemy.Column(sqlalchemy.types.Integer, sqlalchemy.ForeignKey(TefObject.Id), primary_key=True) barCode = sqlalchemy.Column( sqlalchemy.types.String(12), nullable=False) class Location(PhysObject): Id = sqlalchemy.Column(sqlalchemy.types.Integer, sqlalchemy.ForeignKey(PhysObject.Id), primary_key=True) description = sqlalchemy.Column( sqlalchemy.types.String(128)) PhysObject.locationId = sqlalchemy.orm.column_property( sqlalchemy.Column( 'locationId', sqlalchemy.types.Integer, sqlalchemy.ForeignKey('Location.Id', name = 'physobject_location_fkey', use_alter = True) ) ) # this line fixes the error: # PhysObject.__mapper__._configure_inheritance() PhysObject.location = sqlalchemy.orm.relation( Location, primaryjoin = PhysObject.locationId == Location.__table__.c.Id, foreign_keys = [PhysObject.locationId], remote_side = [Location.__table__.c.Id] ) room = Location(description = 'room 1', barCode = '1234') desk = PhysObject(barCode='555', location = room) ===================================================================== I want to put definining .locationId and .location into one function, so I need to assing .locationId outside of the PhysObject class definition. Is using Mapper._configure_inheritance() a proper approach? thanks and bye, Filip Zyzniewski --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---