dynamically creating a set of SQLAlchemy table

if you want to create multiple table objects, you could create a function to generate and return a new class. Maybe something like this:
python


Base = declarative_base()

def TableCreator(tablename):
    class MyTable(Base):
        __tablename__ = tablename
        row_id = Column(INT, primary_key=True)
        another_column = Column(CHAR(10))
    return MyTable
Then you could call it with mytable1 = TableCreator("mytable1").
相關文章
相關標籤/搜索