Python 通过函数创建Sqlalchemy数据库

Python 通过函数创建Sqlalchemy数据库,python,sqlalchemy,Python,Sqlalchemy,现在我想在函数外部创建一个数据库 base,tableA,tableB=创建表格(“表格a”) 现在的问题是如何创建这个数据库 当我试着运行它时,我得到了这个 sqlalchemy.exc.NorReferencedColumnError:无法初始化目标 表“table_b”上ForeignKey“table_a.\u content_id”的列: 表“table_a”没有名为“\u content\u id”的列 感谢您抽出时间:) def create_tables(name_of_tab

现在我想在函数外部创建一个数据库

base,tableA,tableB=创建表格(“表格a”)

现在的问题是如何创建这个数据库

当我试着运行它时,我得到了这个

sqlalchemy.exc.NorReferencedColumnError:无法初始化目标 表“table_b”上ForeignKey“table_a.\u content_id”的列: 表“table_a”没有名为“\u content\u id”的列

感谢您抽出时间:)

def create_tables(name_of_table_a):

    Base = declarative_base()

    class A(Base):
        __tablename__ = name_of_table_a
        table_id = Column(“table_id", Integer, primary_key=True) 
        _content_id = Column("content_id", Integer, primary_key=False)
        _title = Column("title", String, primary_key=False)
       

        @property
        def content_id(self):
            self._content_id

        @content_id.setter
        def content_id(self, value):
            self._content_id = value

        @property
        def title(self):
            self._title

        @title.setter
        def title(self, value):
            self._title = value
     

    class B(Base):

        __tablename__ = “table_b"
        _content_id = Column(
            "content_id", Integer, ForeignKey(name_of_table_a +"._content_id")
        )
        _image_id = Column("image_id", Integer, primary_key=True)

        @property
        def content_id(self):
            self._content_id

        @content_id.setter
        def content_id(self, value):
            self._content_id = value

        @property
        def image_id(self):
            self._image_id

        @image_id.setter
        def image_id(self, value):
            self._image_id = value

    return Base, A, B