How to delete a table in SQLAlchemy?

前端 未结 4 909
无人共我
无人共我 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 16:04

    Just call drop() against the table object. From the docs:

    Issue a DROP statement for this Table, using the given Connectable for connectivity.

    In your case it should be:

    User.__table__.drop()
    

    If you get an exception like:

    sqlalchemy.exc.UnboundExecutionError: Table object 'my_users' is not bound to an Engine or Connection. Execution can not proceed without a database to execute against
    

    You need to pass the engine:

    User.__table__.drop(engine)
    

提交回复
热议问题