O!My Models (omymodels) is a library to generate Pydantic, Dataclasses, GinoORM Models, SqlAlchemy ORM, SqlAlchemy Core Table, Models from SQL DDL. And convert
O!My Models (omymodels) is a library to generate Pydantic, Dataclasses, GinoORM Models, SqlAlchemy ORM, SqlAlchemy Core Table, Models from SQL DDL. And convert
Interactive Demo | Documentation | PyPI
Try the online O!MyModels converter or simply use it online: https://archon-omymodels-online.hf.space/ (A big thanks for that goes to https://github.com/archongum)
You can find usage examples in the example/ folder on GitHub: https://github.com/xnuinside/omymodels/tree/main/example
O! My Models (omymodels) is a library that allow you to generate different ORM & pure Python models from SQL DDL or convert one models type to another (exclude SQLAlchemy Table, it does not supported yet by py-models-parser).
Supported Models:
Mapped and mapped_column
pip install omymodels
By default method create_models generates GinoORM models. Use the argument models_type to specify output format:
'pydantic' - Pydantic v1 models (uses Optional[X])'pydantic_v2' - Pydantic v2 models (uses X | None syntax, dict | list for JSON)'sqlalchemy' - SQLAlchemy ORM models'sqlalchemy_core' - SQLAlchemy Core Tables'dataclass' - Python Dataclasses'sqlmodel' - SQLModel models'openapi3' - OpenAPI 3 (Swagger) schema definitionsA lot of examples in tests/ - https://github.com/xnuinside/omymodels/tree/main/tests.
……Key differences in Pydantic v2 output:
X | None instead of Optional[X]dict | list for JSON/JSONB types instead of Jsonfrom __future__ import annotations for Python 3.9 compatibility= None defaultTo generate Dataclasses from DDL use argument models_type='dataclass'
for example:
# (same DDL as in Pydantic sample)
result = create_models(ddl, schema_global=False, models_type='dataclass')['code']
# and result will be:
import datetime
from dataclasses import dataclass
@dataclass
class UserHistory:
id: str
user: str
status: str
runid: int = None
job_id: int = None
event_time: datetime.datetime = datetime.datetime.now()
comment: str = 'none'GinoORM example. If you provide an input like:
CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY,
"name" varchar,
"created_at" timestamp,
"updated_at" timestamp,
"country_code" int,
"default_language" int
);
CREATE TABLE "languages" (
"id" int PRIMARY KEY,
"code" varchar(2) NOT NULL,
"name" varchar NOT NULL
);
and you will get output:
…
omm path/to/your.ddl
# for example
omm tests/test_two_tables.sql
You can define target path where to save models with -t, --target flag:
# for example
omm tests/test_two_tables.sql -t test_path/test_models.py
If you want generate the Pydantic or Dataclasses models - just use flag -m or --models_type='pydantic' / --models_type='dataclass'
omm /path/to/your.ddl -m dataclass
# or
omm /path/to/your.ddl --models_type pydantic
Small library is used for parse DDL- https://github.com/xnuinside/simple-ddl-parser.
First of all, to parse types correct from DDL to models - they must be in types mypping, for Gino it exitst in this file:
omymodels/gino/types.py types_mapping
If you need to use fast type that not exist in mapping - just do a path before call code with types_mapping.update()
for example:
from omymodels.models.gino import types
from omymodels import create_models
types.types_mapping.update({'your_type_from_ddl': 'db.TypeInGino'})
ddl = "YOUR DDL with your custom your_type_from_ddl"
models = create_models(ddl)
#### And similar for Pydantic types
from omymodels.models.pydantic import types types_mapping
from omymodels import create_models
types.types_mapping.update({'your_type_from_ddl': 'db.TypeInGino'})
ddl = "YOUR DDL with your custom your_type_from_ddl"
models = create_models(ddl, models_type='pydantic')There is 2 ways how to define schema in Models:
from gino import Gino
db = Gino(schema="schema_name")
And this is a default way for put schema during generation - it takes first schema in tables and use it.
--no-global-schema if you use cli or put argument 'schema_global=False' to create_models() function if you use library from code. Like this:
ddl = """
CREATE TABLE "prefix--schema-name"."table" (
_id uuid PRIMARY KEY,
one_more_id int
);
create unique index table_pk on "prefix--schema-name"."table" (one_more_id) ;
create index table_ix2 on "prefix--schema-name"."table" (_id) ;
"""
result = create_models(ddl, schema_global=False)And result will be this:
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.schema import UniqueConstraint
from sqlalchemy import Index
from gino import Gino
db = Gino()
class Table(db.Model):
__tablename__ = 'table'
_id = db.Column(UUID, primary_key=True)
one_more_id = db.Column(db.Integer())
__table_args__ = (
UniqueConstraint(one_more_id, name='table_pk'),
Index('table_ix2', _id),
dict(schema="prefix--schema-name")
)O!MyModels supports bidirectional conversion with OpenAPI 3 schemas.
……YAML schemas are also supported (requires pyyaml):
pip install pyyamlYou can add support for your own model types without forking the repository.
…from omymodels import register_generator
from omymodels.models.pydantic_v2.core import ModelGenerator as PydanticV2Generator
class CustomPydanticGenerator(PydanticV2Generator):
def create_header(self, *args, **kwargs):
header = super().create_header(*args, **kwargs)
return "from my_types import CustomType\n" + header
register_generator("my_pydantic", CustomPydanticGenerator)See full examples in example/custom_generator.py and example/extend_builtin_generator.py.
Please describe issue that you want to solve and open the PR, I will review it as soon as possible.
Any questions? Ping me in Telegram: https://t.me/xnuinside or mail [email protected]
If you see any bugs or have any suggestions - feel free to open the issue. Any help will be appritiated.
One more time, big 'thank you!' goes to https://github.com/archongum for Web-version: https://archon-omymodels-online.hf.space/
See CHANGELOG.md for full version history.
Breaking Changes:
New Features:
X | None, dict | list)Improvements:
See CHANGELOG.md for complete details and previous versions.
No open issues yet, or sync has not completed.