Turn off a warning in sqlalchemy

前端 未结 2 810
孤独总比滥情好
孤独总比滥情好 2020-12-15 04:11

I\'m using sqlalchemy with reflection, a couple of partial indices in my DB make it dump warnings like this:

SAWarning: Predicate of partial index i_some_index

2条回答
  •  再見小時候
    2020-12-15 04:24

    Python's warning module provides a handy context manager that catches warnings for you.

    Here's how to filter out the SQLAlchemy warning.

    import warnings
    from sqlalchemy import exc as sa_exc
    
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=sa_exc.SAWarning)
        # code here...
    

    As for development vs production, you can just have this warning wrap around your application's entry point or an external script that invokes your application in your production environment.

    Usually, I solve this by having an environment variable that executes a slightly different code path than when developing, for example, wrapping around different middleware etc.

提交回复
热议问题