On Oct 26, 2011, at 1:04 PM, James Hartley wrote: > On Wed, Oct 26, 2011 at 2:22 AM, Stefano Fontanelli <s.fontane...@asidev.com> > wrote: > > Hi James, > you cannot define two mapper properties that use the same name. > > Thank you. I truly appreciate your response. I can now implement the table > interconnection while both classes are defined in the same file. My problem > is when the classes are separated. Given the following two files: > ========8<---------------------- > #!/usr/bin/env python > """ user.py """ > from sqlalchemy import create_engine, Column, ForeignKey, Integer, String > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy.orm import sessionmaker, relationship, backref > > Base = declarative_base() > > class User(Base): > __tablename__ = 'users' > > from user import User > > Base = declarative_base() > > class Address(Base): > __tablename__ = 'addresses' > > id = Column(Integer, primary_key=True) > email_address = Column(String, nullable=False) > user_id = Column(Integer, ForeignKey('users.id'), nullable=False) > > user = relationship('User', backref=backref('addresses', order_by=id))
If you wish to locate classes based on their string name as you are doing in relationship('User') here, the calling class (Address) must share the same registry of names that the desired class (User) does. This registry is part of the "Base". Therefore your entire application needs to have exactly one usage of "declarative_base()", where all descending classes use the same Base object, and not one usage per file. Additionally, both user.py and address.py must be imported via the Python "import" statement before the mappings can be used. -- 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.