Generate SQLModel code from SQL
Project description
sqlmodelgen
sqlmodelgen is a library to generate models for the sqlmodel library (repo, official docs).
It accepts in input the following sources:
- direct
CREATE TABLEsql statements - sqlite file path
- postgres connection string
Installation
Available on PyPi, just run pip install sqlmodelgen
Code generation from postgres requires the separate postgres extension, installable with pip install sqlmodelgen[postgres]
Usage
Generating from CREATE TABLE
from sqlmodelgen import gen_code_from_sql
sql_code = '''
CREATE TABLE Hero (
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
secret_name VARCHAR NOT NULL,
age INTEGER,
PRIMARY KEY (id)
);
print(gen_code_from_sql(sql_code))
'''
generates:
from sqlmodel import SQLModel, Field
class Hero(SQLModel, table = True):
__tablename__ = 'Hero'
id: int = Field(primary_key=True)
name: str
secret_name: str
age: int | None
Generating from SQLite
from sqlmodelgen import gen_code_from_sqlite
code = gen_code_from_sqlite('/home/my_user/my_database.sqlite')
Generating from Postgres
The separate postgres extension is required, it can be installed with pip install sqlmodelgen[postgres].
from sqlmodelgen import gen_code_from_postgres
code = gen_code_from_postgres('postgres://USER:PASSWORD@HOST:PORT/DBNAME')
Relationships
sqlmodelgen allows to build relationships by passing the argument generate_relationships=True to the functions:
gen_code_from_sqlgen_code_from_sqlitegen_code_from_postgres
In such case sqlmodelgen is going to generate relationships between classes based on the foreign keys retrieved.
The following example
schema = '''CREATE TABLE nations(
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE athletes(
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
nation_id BIGSERIAL,
FOREIGN KEY (nation_id) REFERENCES nations(id)
);'''
sqlmodel_code = gen_code_from_sql(schema, generate_relationships=True)
will generate:
from sqlmodel import SQLModel, Field, Relationship
class Nations(SQLModel, table = True):
__tablename__ = 'nations'
id: int | None = Field(primary_key=True)
name: str
athletess: list['Athletes'] = Relationship(back_populates='nation')
class Athletes(SQLModel, table = True):
__tablename__ = 'athletes'
id: int | None = Field(primary_key=True)
name: str
nation_id: int | None = Field(foreign_key="nations.id")
nation: Nations | None = Relationship(back_populates='athletess')
Internal functioning
The library relies on sqloxide to parse SQL code, then generates sqlmodel classes accordingly
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlmodelgen-0.0.13.tar.gz.
File metadata
- Download URL: sqlmodelgen-0.0.13.tar.gz
- Upload date:
- Size: 43.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.30
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bca291578cbd9953adac39cb32379d9c26e8622a3455df3bfe132a7dbb3fd04c
|
|
| MD5 |
89bd9cd3a608ec945964d8f08f5725b1
|
|
| BLAKE2b-256 |
00dc3cc2ee165e4b5fd28df1983e54cdab60f3428bc8170bfaab5bbc756d0094
|
File details
Details for the file sqlmodelgen-0.0.13-py3-none-any.whl.
File metadata
- Download URL: sqlmodelgen-0.0.13-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.30
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34f988a116aadbce3cea33bd5a70a130044e4dcd89dd3859ec3da1ef4edfbc20
|
|
| MD5 |
5e2b4641812f341934a80c9034c06423
|
|
| BLAKE2b-256 |
50a8c863c3f979650882eb926bc402f0d96ee020feb6f6101df64ea324df4dbf
|