How to automatically generate SQLAlchemy models for MySQL

install dependency:

pip install sqlacodegen pymysql

example command

Then you can generate models like this:

sqlacodegen mysql+pymysql://<user>:<password>@localhost:3306/<db>

Remember to replace placeholders like <user>.

If you don't need indexes in the output, use --noindexes option:

sqlacodegen --noindexes mysql+pymysql://<user>:<password>@localhost:3306/<db>

This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/auto_sqlalchemy_models

Posted on 2022-03-25 Mail to author