Skip to main content

Generate SQLModel code from SQL

Project description

sqlmodelgen

Coverage badge PyPI version

sqlmodelgen is a library to generate models for the sqlmodel library (repo, official docs).

It accepts in input the following sources:

  • direct CREATE TABLE sql statements
  • sqlite file path
  • postgres connection string
  • mysql connection from the mysql-connector-python library

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')

Generating from MYSQL

The separate mysql extension is required, it can be installed with pip install sqlmodelgen[mysql].

import mysql.connector
from sqlmodelgen import gen_code_from_mysql

# instantiate your connection
conn = mysql.connector.connect(host='YOURHOST', port=3306, user='YOURUSER', password='YOURPASSWORD')

code = gen_code_from_mysql(conn, 'YOURDBNAME')

Relationships

sqlmodelgen allows to build relationships by passing the argument generate_relationships=True to the functions:

  • gen_code_from_sql
  • gen_code_from_sqlite
  • gen_code_from_postgres
  • gen_code_from_mysql

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


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.17.tar.gz (53.3 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.17-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file sqlmodelgen-0.0.17.tar.gz.

File metadata

  • Download URL: sqlmodelgen-0.0.17.tar.gz
  • Upload date:
  • Size: 53.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.30

File hashes

Hashes for sqlmodelgen-0.0.17.tar.gz
Algorithm Hash digest
SHA256 97e1f59ca2882892c6278f20a1af6fb07f295785d53e93ff4a7c151fbd6356f5
MD5 8638e46b7c3d07d5775d668304be442a
BLAKE2b-256 972a5929aa06b1dd048dfe1d1c42dde3fa4e567c7ad7cb6fb8b6528ffc8e778e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlmodelgen-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 d1c8650c974d1ca508d1411d782eb6b5d51350e1f4d1f619e2fc70c723d6e4c6
MD5 c559fbfed7fbe6152cd0b4e5282f94a2
BLAKE2b-256 767555400631f8a74c09968842cd84408160bb8dc5c044e25809705e13431e1d

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