# sqlmodelgen

A Python library and command-line tool that generates SQLModel model classes from CREATE TABLE statements, a SQLite file, or a Postgres or MySQL connection, optionally inferring relationships from foreign keys.

sqlmodelgen generates Python classes for the sqlmodel library from an existing schema. It accepts direct CREATE TABLE SQL statements, a SQLite file path, a Postgres connection string, or a MySQL connection object from the mysql-connector-python library, and exposes a matching function for each source, such as gen_code_from_sql, gen_code_from_sqlite, gen_code_from_postgres and gen_code_from_mysql.

It installs from PyPI, with separate optional extras for the Postgres and MySQL sources so their driver dependencies are not required for the base SQLite and raw-SQL cases. It can be used both as a command-line tool and as a library called from Python code.

Passing generate_relationships=True to any of the generator functions makes sqlmodelgen also infer and generate relationships between the produced classes based on the foreign keys found in the source schema.

Website: https://github.com/nucccc/sqlmodelgen
