Hi,
I have a problem with such program (available at
http://filip.math.uni.lodz.pl/relation_problem.py):
=========================================================================
Fails with Python-2.5.4 and SQLAlchemy-0.5.5
import sqlalchemy
import sqlalchemy.ext.declarative
'''
This program fails with:
==================================================================
File
"/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.5-py2.5.egg/sqlalchemy/orm/properties.py",
line 953, in _determine_direction
"argument." % self)
sqlalchemy.exc.ArgumentError: Can't determine relation direction
for relationship 'PhysObject.location' - foreign key columns are present
in both the parent and the child's mapped tables. Specify
'foreign_keys' argument.
==================================================================
Note that this fails for PhysObject.location, but doesn't for
TefObject.creator which is practically identical.
I have tracked it down to Comparator._refers_to_parent_table().
For TefObject.Id and TefObject.creatorId (building relation
TefObject.creator) it returns true (same table),
but not for TefObject.Id and PhysObject.locationId (building relation
PhysObject.location).
'''
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)
creatorId = sqlalchemy.Column(sqlalchemy.types.Integer,
sqlalchemy.ForeignKey('User.Id', name = 'tefobject_user_fkey', use_alter
= True))
__mapper_args__ = {'polymorphic_on': objectType}
class User(TefObject):
Id = sqlalchemy.Column(sqlalchemy.types.Integer,
sqlalchemy.ForeignKey(TefObject.Id), primary_key=True)
name = sqlalchemy.Column( sqlalchemy.types.String(128))
TefObject.creator = sqlalchemy.orm.relation(
User,
primaryjoin = TefObject.creatorId == User.Id,
foreign_keys = [TefObject.creatorId],
remote_side = [User.Id]
)
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)
locationId = sqlalchemy.Column(sqlalchemy.types.Integer,
sqlalchemy.ForeignKey('Location.Id', name = 'physobject_location_fkey',
use_alter = True))
class Location(PhysObject):
Id = sqlalchemy.Column(sqlalchemy.types.Integer,
sqlalchemy.ForeignKey(PhysObject.Id), primary_key=True)
description = sqlalchemy.Column( sqlalchemy.types.String(128))
PhysObject.location = sqlalchemy.orm.relation(
Location,
primaryjoin = PhysObject.locationId == Location.Id,
foreign_keys = [PhysObject.locationId],
remote_side = [Location.Id]
)
joe = User(name = 'Joe')
stuff = TefObject(creator = joe)
room = Location(description = 'room 1', barCode = '1234')
desk = PhysObject(barCode='555', location = room)
=========================================================================
Is it my mistake or a bug in SQLAlchemy?
Filip Zyzniewski
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---