Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 1.09 KB

README.md

File metadata and controls

47 lines (33 loc) · 1.09 KB

sqlmodelgen

sqlmodelgen is a library to convert CREATE TABLE statements from SQL to classes inheriting SQLModel from the famous sqlmodel library.

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

Internal functioning

The library relies on sqloxide to parse SQL code, then generates sqlmodel classes accordingly

Possible improvements

  • Support for more SQL data types
  • Possibility to acquire in input actual database connections (like Postgres) or files (SQLite) and generate sqlmodel code accordingly