如何为Sqlalchemy使用类

2024-09-28 19:10:58 发布

您现在位置:Python中文网/ 问答频道 /正文

你能帮帮我吗?我使用sqlalchemy来反映表格。在两个模块中,我对类GenePanel的定义相同,但基类不同 对于我的第一个模块,我有以下内容:

from ..db import Base_of_first_module
from .mixins import TimeStampMixin


class GenePanel(Base_of_first_module, TimeStampMixin):
    __tablename__ = "gene_panel"
    __table_args__ = {"schema": "core"}

    panel_id = Column(Integer, primary_key=True)
    project_id = Column(ForeignKey("core.project.project_id", deferrable=True))
    base_panel_id = Column(ForeignKey("core.gene_panel.gene_panel_id", deferrable=True), index=True)
    title = Column(Text, nullable=False)

对于我的第二个模块,我可以写以下内容,它将起作用

from second_module.db import Base_of_second_module 
from .mixins import TimeStampMixin 
class GenePanel(Base_of_second_module, TimeStampMixin): 
    __tablename__ = "gene_panel" 
    __table_args__ = {"schema": "core"} 
    panel_id = Column(Integer, primary_key=True) 
    project_id = Column(ForeignKey("core.project.project_id", deferrable=True)) 
    base_panel_id = Column(ForeignKey("core.gene_panel.gene_panel_id", deferrable=True), index=True) title = Column(Text, nullable=False)    

我试图将GenePanel类导入到第二个模块中。如何在第二个模块中声明正确的基类? (在我的第二个模块GenePanel.__bases__(Base_of_second_module)中执行类似的操作),但它没有以正确的方式工作

from second_module.db import Base_of_second_module 
from .mixins import TimeStampMixin 
from first_module import Gene_Panel 

GenePanel.__bases__(Base_of_second_module)

Tags: 模块offromcoreimportprojectidtrue