Error creating backref

1,740 views
Skip to first unread message

Michael Hipp

unread,
Aug 6, 2010, 10:04:50 PM8/6/10
to sqlal...@googlegroups.com
Can someone help me figure out why I keep getting the error below. Here
are my 3 models. It's a simple many-one on banks-dealer and reps-dealer.

class Dealer(Base):
__tablename__ = 'dealers'
id_ = Column(Integer, primary_key=True)
reps = relationship('Rep', order_by='Rep.lname', backref="dealer")
banks = relationship('Bank', order_by='Bank.id_', backref="dealer")

class Bank(Base):
__tablename__ = 'banks'
id_ = Column(Integer, primary_key=True)
dealer_id = Column(Integer, ForeignKey('dealers.id_'))
dealer = relationship(Dealer, backref=backref('banks', order_by=id_))

class Rep(Base):
__tablename__ = 'reps'
id_ = Column(Integer, primary_key=True)
dealer_id = Column(Integer, ForeignKey('dealers.id_'))
dealer = relationship(Dealer, backref=backref('reps', order_by=id_))

I'm attempting to do this exactly like:
http://www.sqlalchemy.org/docs/ormtutorial.html#building-a-relationship

But when I run it it gives:
sqlalchemy.exc.ArgumentError: Error creating backref 'dealer' on
relationship 'Dealer.reps': property of that name exists on mapper
'Mapper|Rep|reps'

Any help?
Thanks,
Michael

Michael Hipp

unread,
Aug 7, 2010, 1:46:37 PM8/7/10
to sqlal...@googlegroups.com

I think I figured it out, it's not necessary to put the backref on both ends.

Michael

Mark

unread,
Aug 8, 2010, 5:35:11 AM8/8/10
to sqlalchemy

Yes that's true. If you declared the backrefs in dealer, you do not
need to declare it again in the two classes below.

Hope this helps. Sorry about my briefness, I'm typing this on my
phone.

-Mark
Reply all
Reply to author
Forward
0 new messages