简体   繁体   English

SQLAlchemy 导入关系表

[英]SQLAlchemy import tables with relationships

I have problem with separating tables with relationships in different files.我在使用不同文件中的关系分隔表时遇到问题。 I want the tables below to be in three separate files and to import TableA in third party page, but I can not manage the load order.我希望下表位于三个单独的文件中,并在第三方页面中导入TableA ,但我无法管理加载顺序。

In most of the time I'm receiving the following error.在大多数情况下,我会收到以下错误。

sqlalchemy.exc. sqlalchemy.exc。 InvalidRequestError: When initializing mapper Mapper|TableA|tablea, expression 'TableB' failed to locate a name ("name 'TableB' is not defined"). InvalidRequestError:初始化映射器 Mapper|TableA|tablea 时,表达式“TableB”无法找到名称(“名称‘TableB’未定义”)。 If this is a class name, consider adding this relationship() to the class after both dependent classes have been defined.如果这是一个 class 名称,请考虑在定义两个依赖类后将此 relationship() 添加到 class。

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)

This should work (note that the TableC. table is replaced with the name of the table to avoid circular module loading): 这应该工作(注意TableC。 被替换为表的名称,以避免循环模块加载):

### 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)

Also I believe that the ForeignKey parameter is case sensitive, so you code might not work because "TableA.id" doe snot match "tablea" name when case-sensitive. 另外我认为ForeignKey参数区分大小写,因此您的代码可能无效,因为“TableA.id”在区分大小写时会与“tablea”名称匹配。

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

Simply modify the first line for 'ForeignKey' class and add another for the relationship class like below: 只需修改'ForeignKey'类的第一行,然后为关系类添加另一行,如下所示:

from sqlalchemy import Integer, ForeignKey, String, Column

from sqlalchemy.orm import relationship

To turn off the modification warnings, add another line 要关闭修改警告,请添加另一行

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

Change the 'tableab' table name to 'tableb', this is the reason it is giving error that tableB is not found. 将'tableab'表名更改为'tableb',这就是它找不到tableB的错误的原因。 Moreover, Python is case sensitive and we have to remember this. 此外,Python区分大小写,我们必须记住这一点。

Hope this will suffice. 希望这就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM