Search code examples
pythondebuggingloggingsqlalchemysqlmodel

How can I log sql queries in the SqlModel?


How could I see/log queries sent by sqlmodel to database.


Solution

  • Since sqlmodel uses Sqlalchemy as backend ORM engine, we can use Sqlalchemy logging answer like answer here:

    import logging
    logging.basicConfig()
    logger = logging.getLogger('sqlalchemy.engine')
    logger.setLevel(logging.DEBUG)
    # run sqlmodel code after this
    

    You should be able to see sql queries in the console.