Skip to main content

sqlagen

Model Generator from projects using SQLAlchemy as their orm.

This package is helpful while building web apps with FastAPI and SQLAlchemy.

It provides capability which can:

  • generate SQLAlchemy from database schema
  • generates Pydantic Model from SQLAlchemy Model.

Project was originally forked from https://github.com/agronholm/sqlacodegen

However, we design to modify some of the features and add new features to the project to fit our own project needs, so we decided to create a new project.

Installation

To install,

pip install sqlagen

Quickstart

Please be aware that we are using pydantic >= 2.0.0 and sqlalchemy >= 2.0 in this project.

Generate SQLAlchemy Models

You may use generate_db_models function directly to generate Pydantic Model from SQLAlchemy Model. It takes Generator's init arguments and init a Generator class.

Examples:

from sqlagen import generate_db_models

generate_db_models(db_url="pymysql+mysql://localhost:6306/test")
from sqlagen import DeclarativeGenerator
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import MetaData

metadata = MetaData()
engine = create_engine("pymysql+mysql://localhost:6306/test")
generator = DeclarativeGenerator(metadata=metadata, engine=engine, options={})
print(generator.generate())

Generate Pydantic Schema Models

You may use generate_db_models function directly to generate Pydantic Model from SQLAlchemy Model. It takes Generator's init arguments and init a Generator class.

Examples:

from sqlagen import generate_schema_models
    
generate_schema_models(models=[MyModel], base_model=CustomBaseModel)
from sqlagen import SchemaModelGenerator

generator = SchemaModelGenerator(base_model=CustomBaseModel)
generator.generate_from_module(models=my_models_module, output_path="schemas.py")

Generator Class takes following init arguments

  • split_models: Whether to split models into Base, Create, Update and Read models. Default is Fakse.
  • base_model: Base model to inherit from. Default is BaseModel from pydantic.
  • restrict_fields: Which takes a set of fields to restrict. Default is None. This is useful when you want to restrict some fields to be readonly such as id, created_at, updated_at.
  • indentation: Indentation to use in generated code.

More Examples

Since most of our projects use sqlalchemy and pydantic, so we often need to generate models from database schema and generate pydantic models from sqlalchemy models.

from sqlagen import generate_db_models, SchemaModelGenerator

generate_db_models(db_url="pymysql+mysql://localhost:6306/test",outfile_path="./models")
generate_schema_models(models=[MyModel], base_model=CustomBaseModel, outfile_path="./schemas")

RoadMap

  • Strict typing, such as using conint for limiting Integer size and constr for String length.
  • Probably, generate relationships as well.

Metadata

Release files for sqlagen 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlagen 0.1.3
File Size Uploaded
sqlagen-0.1.3.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlagen 0.1.3
File Interpreter ABI Platform
sqlagen-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 48.0 kB

Release files / sqlagen-0.1.3.tar.gz

Download URL sqlagen-0.1.3.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5d3e46e72c793c18737aab46a74751f94ed3ee66932fe9c0706317df1faa4e18
BLAKE2b-256 checksum
How to use checksums
bc5ccef35ddf5509849954d377da96cda10b4f022b751cf7bfd5f325280a25ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.0 Darwin/23.2.0

Release files / sqlagen-0.1.3-py3-none-any.whl

Download URL sqlagen-0.1.3-py3-none-any.whl
Size 26.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf105e7f784dd2a1710f99d0fbf1a2358f968911855d7223c02b9f302fec1ffc
BLAKE2b-256 checksum
How to use checksums
2de685cd6826825a42866a8eee23901ed82001380c92d34857413c65218f1415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.0 Darwin/23.2.0

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page