0

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)

Edited by <HHH>

2
Contributors
2
Replies
37
Views
11 Months
Discussion Span
Last Post by <HHH>
0

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

address = relationship ("address", back_populates = 'user')
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.