SQLAlchemy imports relationship tables

I have a problem with splitting tables with relationships in different files. I want the tables below to be in three separate files and import TableA to a third-party page, but I cannot control the loading order.

In most cases, I get the following error.

sqlalchemy.exc. InvalidRequestError: when initializing mapper Mapper | TableA | tablea, expression "TableB" could not find the name ("name" TableB "is not defined"). If it is a name class, consider adding this relationship () to the class after defining both dependent classes.

class TableA(Base): __tablename__ = "tablea" id = Column(Integer, primary_key=True) name = Column(String) tableB = relationship("TableB", secondary = TableC.__table__) class TableB(Base): __tablename__ = "tableb" id = Column(Integer, primary_key=True) name = Column(String) class TableC(Base): __tablename__ = "tableab" tableAId = Column("table_a_id", Integer, ForeignKey("TableA.id"), primary_key=True) tableBId = Column("table_b_id", Integer, ForeignKey("TableB.id"), primary_key=True) 
+12
python sqlalchemy relationship
source share
3 answers

This should work (note that Table. replaced by the table name to avoid loading the cyclic module):

 ### base.py engine = create_engine('sqlite:///:memory:', echo=True) Session = sessionmaker(bind=engine) Base = declarative_base(bind=engine) ### classA.py from base import Base from classB import TableB class TableA(Base): __tablename__ = 'tablea' id = Column(Integer, primary_key=True) name = Column(String(50)) tableBs = relationship("TableB", secondary="tableab") #tableBs = relationship("TableB", secondary=TableC.__table__) ### classB.py from base import Base class TableB(Base): __tablename__ = 'tableb' id = Column(Integer, primary_key=True) name = Column(String(50)) ### classC.py from base import Base from classA import TableA from classB import TableB class TableC(Base): __tablename__ = 'tableab' tableAId = Column(Integer, ForeignKey("tablea.id"), primary_key=True, ) tableBId = Column(Integer, ForeignKey("tableb.id"), primary_key=True, ) ### main.py from base import Base, Session, engine from classA import TableA from classB import TableB from classC import TableC Base.metadata.create_all(engine) 

I also think that the ForeignKey parameter is case sensitive, so the code may not work because the sno table file "TableA.id" matches the name "tablea" when it is case sensitive.

+7
source share
 from sqlalchemy import Column, String, Integer from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Parent(Base): __tablename__ = 'Parent' ParentID = Column(Integer, primary_key=True) Description = Column(String) def __init__(self, ParentID, Description): self.ParentID = ParentID self.Description = Description ---------------------------------------------------------------------- from sqlalchemy import Column, String, Integer, ForeignKey import Parent from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Child(Base): __tablename__ = "Child" ChildID = Column(Integer, primary_key=True) Description = Column(String) ParentID = Column('CompanyID', Integer, ForeignKey(Parent.ParentID)) def __init__(self, ChildID, Description,ParentID): self.ChildID = ChildID self.Description = Description self.ParentID=ParentID 
+1
source share

Just change the first line for the ForeignKey class and add another for the relationship class, as shown below:

 from sqlalchemy import Integer, ForeignKey, String, Column from sqlalchemy.orm import relationship 

To disable change alerts, add another line

 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True 

Change the name of the table 'tableab' to 'tableb', for this reason it throws an error since tableB was not found. In addition, Python is case sensitive, and we must keep this in mind.

I hope this will be enough.

-one
source share

All Articles