1. Engineering
  2. Computer Science
  3. got error for python app sqlalchemyexcinvalidrequesterror one or more mappers...

Question: got error for python app sqlalchemyexcinvalidrequesterror one or more mappers...

Question details

got error for python app

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|Room|room'. Original exception was: relationship 'building' expects a class or a mapper argument (received: <class 'sqlalchemy.sql.schema.Table'>)

heres my code for the db

models.py

from app import db


class Building(db.Model):
__tablename__ = "building"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)

def __repr__(self):
return "<Building: {}>".format(self.name)


class Room(db.Model):
""""""
__tablename__ = "room"

id = db.Column(db.Integer, primary_key=True)
roomnumber = db.Column(db.String)
ranges = db.Column(db.String)
room_type = db.Column(db.String)

building_id = db.Column(db.Integer, db.ForeignKey("building.id"))
building = db.relationship("building", backref=db.backref(
"room", order_by=id), lazy=True)

***********************************************************************

make_db.py

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref


engine = create_engine('sqlite:///ucsb_room.db', echo=True)
Base = declarative_base()

class Building(Base):
__tablename__ = "building"

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

def __repr__(self):
return "{}".format(self.name)


class Room(Base):
""""""
__tablename__ = "room"

id = Column(Integer, primary_key=True)
roomnumber = Column(String)
ranges = Column(String)
room_type = Column(String)

building_id = Column(Integer, ForeignKey("building.id"))
building = relationship("building", backref=backref(
"room", order_by=id))

Base.metadata.create_all(engine)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution