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>
Posted on 2022-03-25