Hi everyone, I am hitting an error while defining a relationship with a parent table with a composite primary key (my child class has two attributes that point to the same parent class -with a composite pk- but both relationships are properly defined with the 'foreign_keys' parameter): I am not sure the reason I get an AmbiguousForeignKeysError, I have followed the docs and tried several alternatives to no avail. Probably some typo or a silly mistake like that but I am unable to see it. I would appreciate a pointer.

first, here is the error message I am getting:

File "/home/mariano/Code/n-p/env/lib/python3.3/site-packages/SQLAlchemy-0.9.2-py3.3-linux-i686.egg/sqlalchemy/orm/relationships.py", line 1868, in _determine_joins
  % self.prop)
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Bet.runner - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.

and here is a completed self contained example to reproduce the problem:

from sqlalchemy import (
Table, Column, ForeignKey, Index, CheckConstraint, ForeignKeyConstraint,
    Integer, Text, String, Boolean, Date, Enum, Numeric,
    inspect, create_engine,
    and_
    )

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    relationship
    )

DBSession = scoped_session(sessionmaker())

Base = declarative_base()

class User(Base):
    __tablename__ = "user"
    uid = Column(String(600), primary_key=True)
    oid = Column(String(100), primary_key=True)
    name = Column(String(600), nullable=False)
    lastname = Column(String(600), nullable=False)

class Bet(Base):
    __tablename__ = "bet"
    __table_args__ = (
        ForeignKeyConstraint(("uid", "oid"), ("user.uid", "user.oid")),
ForeignKeyConstraint(("loader_uid", "oid"), ("user.uid", "user.oid")),
    )
    id = Column(Integer, autoincrement=True, primary_key=True)
    uid = Column(String(600), nullable=False)
    oid = Column(String(100), nullable=False)
    number = Column(Integer, CheckConstraint("number<10000"))
    loader_uid = Column(String(600), nullable=False)

    # relationships
    runner = relationship("User", foreign_keys=[uid, oid],
#primaryjoin=and_("Bet.uid==User.uid", "Bet.oid==User.oid"),
                          )
    loader = relationship("User", foreign_keys=[loader_uid, oid])

Index("runner_idx", Bet.__table__.c.uid, Bet.__table__.c.oid)

if __name__ == "__main__":
    engine = create_engine('sqlite://')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    u1 = User(uid='example1', oid='MDQ', name='Mariano', lastname='Mara')
    u2 = User(uid='example2', oid='MDQ', name='Mariano', lastname='Mara')
    DBSession.add_all([u1, u2])
    DBSession.flush()
    b = Bet(uid='example1', oid='MDQ', number=20, loader_uid='example2')
    DBSession.add(b)
    DBSession.commit()


Thanks in advance

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to