Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlmodelgen-0.0.11.tar.gz (38.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlmodelgen-0.0.11-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

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

Hashes for sqlmodelgen-0.0.11.tar.gz
Algorithm Hash digest
SHA256 9e0f8575e9c8202580c0be41aec7e2b8a3beccc636881634725a2034e4354402
MD5 1b0717e7e252cacce184e6ddf74041d9
BLAKE2b-256 dbbfbe1ea517529bf663a04c3fccd0f36c920a70a49fa936f7b91f2cb6938709

See more details on using hashes here.

File details

Details for the file sqlmodelgen-0.0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlmodelgen-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 49632eafc98f4a1224205565e725ac1ab4d0fcee86b3641a51434c1180353a95
MD5 1f5a2f93e373a507e6e46fd9e70e39b6
BLAKE2b-256 5190eb2eb64ea8c3f506272420d419dbb7ed15765cebeada00f35bd7d650796f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page