Hello

I have created two classes one for the user and the other for address every thing is ok and the tables of the two classes created in my database but when i try to initiate a new object from user class i get the below error

"sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|user|user' has no property 'address"

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import *

db = create_engine('sqlite:///Test.db')

Base = declarative_base()

class user(Base):
    def __init__(self,name,password):
        self.name = name
        self.password = password

    def __repr__(self):
        return "(name = '%s', password = '%s'  )" % (self.name,self.password)

    __tablename__ = 'user'

    id =       Column(Integer , primary_key=True)
    name =     Column(String)
    password = Column(String)

class address(Base):

    __tablename__ = 'address'

    id = Column(Integer,primary_key = True)
    email_add = Column(String,nullable = False)
    user_id = Column(Integer , ForeignKey('user.id'))

    user = relationship ("user", back_populates = 'address')

Base.metadata.create_all(db)

DBsession = sessionmaker(bind = db)
session = DBsession()

user1 = user('hoss',7894)

Look at the example Click Here. Obviously the back_populates value must be the name of one of the other classes attributes.

Thanks Gribouillis the problem was solved by adding the below line in users class

address = relationship ("address", back_populates = 'user')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.