Generate SQLModel code from SQL
Project description
sqlmodelgen
sqlmodelgen is a library to convert CREATE TABLE statements from SQL to classes inheriting SQLModel from the famous sqlmodel library.
At the moment there is support (with limites capabilities) for direct interconnection with SQLite and Postgres
Example
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
Installation
It is already published on PyPi, just type pip install sqlmodelgen
Code generation from postgres requires the separate postgres extension, installable with pip install sqlmodelgen[postgres]
Internal functioning
The library relies on sqloxide to parse SQL code, then generates sqlmodel classes accordingly
Possible improvements
- Support for more SQL data types
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.11.tar.gz.
File metadata
- Download URL: sqlmodelgen-0.0.11.tar.gz
- Upload date:
- Size: 38.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.30
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e0f8575e9c8202580c0be41aec7e2b8a3beccc636881634725a2034e4354402
|
|
| MD5 |
1b0717e7e252cacce184e6ddf74041d9
|
|
| BLAKE2b-256 |
dbbfbe1ea517529bf663a04c3fccd0f36c920a70a49fa936f7b91f2cb6938709
|
File details
Details for the file sqlmodelgen-0.0.11-py3-none-any.whl.
File metadata
- Download URL: sqlmodelgen-0.0.11-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.30
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49632eafc98f4a1224205565e725ac1ab4d0fcee86b3641a51434c1180353a95
|
|
| MD5 |
1f5a2f93e373a507e6e46fd9e70e39b6
|
|
| BLAKE2b-256 |
5190eb2eb64ea8c3f506272420d419dbb7ed15765cebeada00f35bd7d650796f
|