Skip to content

nucccc/sqlmodelgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages