The attached test program generates the following exception with SA
0.2.6. What does the error:

Cant determine relation direction 'set([])'

mean?

Traceback (most recent call last):
 File "test.py", line 56, in ?
   order=Order()
 File "build\bdist.win32\egg\sqlalchemy\orm\mapper.py", line 423, in init
 File "build\bdist.win32\egg\sqlalchemy\orm\mapper.py", line 161, in compile
 File "build\bdist.win32\egg\sqlalchemy\orm\mapper.py", line 387, in
_initialize_properties
 File "build\bdist.win32\egg\sqlalchemy\orm\mapper.py", line 1129, in init
 File "build\bdist.win32\egg\sqlalchemy\orm\properties.py", line 236,
in do_init
 File "build\bdist.win32\egg\sqlalchemy\orm\properties.py", line 298,
in _get_direction
sqlalchemy.exceptions.ArgumentError: Cant determine relation direction
'set([])', for 'order_adjustments' in mapper 'Mapper|Or
derAdjustment|order_adjustment' with primary join
'orders.order_id = order_adjustment.order_id'

The special thing about the test script is that the table
'order_item_adjustment' has foreign key relationships with both the
table 'orders' and 'order_item'. And the column
'order_item_adjustment.order_id' is involved in BOTH of the two
relationships. So maybe SA has difficulty in identifying the column
involved in the relationship and I have to use primaryjoin to
explicitly specify the join condition.

What shall I do to create the relationships correctly?

--
Hong Yuan

大管家网上建材超市
装修装潢建材一站式购物
http://www.homemaster.cn
from sqlalchemy import *

engine=create_engine('sqlite:///:memory:', echo=True)
meta=BoundMetaData(engine)

t_order = Table('orders', meta,
    Column('order_id', Integer, primary_key=True),
    Column('customer_id', Integer),
)

t_order_item = Table('order_item', meta,
    Column('order_id', Integer, ForeignKey('orders.order_id'), primary_key=True),
    Column('order_item_seq_id', Integer, primary_key=True),
    Column('item_name', String(40), nullable=False),
)

t_order_adjustment = Table('order_adjustment', meta,
    Column('order_adjust_seq_id', Integer, primary_key=True),
    Column('order_id', Integer, ForeignKey('orders.order_id'), nullable=False),
    Column('order_item_seq_id', Integer),
    Column('amount', Numeric(11,3)),
    ForeignKeyConstraint(['order_id', 'order_item_seq_id'],
                         ['order_item.order_id', 'order_item.order_item_seq_id'])
)

meta.create_all()

class Order(object): pass
    
class OrderItem(object): pass

class OrderAdjustment(object): pass

mapper(OrderAdjustment, t_order_adjustment)

mapper(OrderItem, t_order_item, properties={
    'item_adjustments': relation(OrderAdjustment,
                                 primaryjoin=
                                 and_(t_order_item.c.order_id==t_order_adjustment.c.order_id,
                                      t_order_item.c.order_item_seq_id==t_order_adjustment.c.order_item_seq_id),
                                 lazy=True)
    })

mapper(Order, t_order, properties={
    'order_items': relation(OrderItem, lazy=True),
    'order_adjustments': relation(OrderAdjustment,
                                  primaryjoin=t_order.c.order_id==t_order_adjustment.c.order_id,
                                  lazy=True)
    })

session = create_session(bind_to=engine)

order=Order()
order.order_id=1

session.save(order)
session.flush()
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to