How to delete a table in SQLAlchemy?

前端 未结 4 912
无人共我
无人共我 2020-12-02 15:23

I want to delete a table using SQLAlchemy.

Since I am testing over and over again, I want to delete the table my_users so that I can start from scratch

4条回答
  •  天命终不由人
    2020-12-02 15:47

    Alternative to calling cls.__table__.drop(your_engine), you can try this:

    Base.metadata.drop_all(bind=your_engine, tables=[User.__table__])
    

    This method as well as the create_all() method accept an optional argument tables, which takes an iterator of sqlalchemy.sql.schema.Table instances.

    You can control which tables are to be created or dropped in this way.

提交回复
热议问题