{"id":14065083,"url":"https://github.com/xnuinside/omymodels","last_synced_at":"2025-04-06T03:07:19.539Z","repository":{"id":41891931,"uuid":"345445535","full_name":"xnuinside/omymodels","owner":"xnuinside","description":"O!My Models (omymodels) is a library to generate Pydantic, Dataclasses, GinoORM Models, SqlAlchemy ORM, SqlAlchemy Core Table,  Models from SQL DDL. And convert one models to another.","archived":false,"fork":false,"pushed_at":"2024-02-11T07:56:02.000Z","size":317,"stargazers_count":155,"open_issues_count":8,"forks_count":17,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-05-07T18:11:39.827Z","etag":null,"topics":["async","code-generation","converter","dataclasses","ddl","ddl-parser","generator","gino","gino-orm","ginoorm-models","hacktoberfest","models","models-generation","orm","orm-model","pydantic","pydantic-models","sql","sqlalchemy"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xnuinside.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-03-07T20:26:28.000Z","updated_at":"2024-05-03T05:41:55.000Z","dependencies_parsed_at":"2024-01-12T23:40:00.229Z","dependency_job_id":"7823bb11-fcd4-4b1f-8a15-97a4b8dd5e96","html_url":"https://github.com/xnuinside/omymodels","commit_stats":{"total_commits":71,"total_committers":6,"mean_commits":"11.833333333333334","dds":0.07042253521126762,"last_synced_commit":"ad3d4c82f4378d91f2db7fe9c23f1eeffb7eba66"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fomymodels","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fomymodels/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fomymodels/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fomymodels/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xnuinside","download_url":"https://codeload.github.com/xnuinside/omymodels/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427006,"owners_count":20937201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","code-generation","converter","dataclasses","ddl","ddl-parser","generator","gino","gino-orm","ginoorm-models","hacktoberfest","models","models-generation","orm","orm-model","pydantic","pydantic-models","sql","sqlalchemy"],"created_at":"2024-08-13T07:04:17.311Z","updated_at":"2025-04-06T03:07:19.514Z","avatar_url":"https://github.com/xnuinside.png","language":"Python","readme":"## O! My Models\n\n![badge1](https://img.shields.io/pypi/v/omymodels) ![badge2](https://img.shields.io/pypi/l/omymodels) ![badge3](https://img.shields.io/pypi/pyversions/omymodels)![workflow](https://github.com/xnuinside/omymodels/actions/workflows/main.yml/badge.svg)\n\n\n## Try in Web-UI \nTry 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)\n\n## Examples \nYou can find usage examples in the example/ folder on GitHub: https://github.com/xnuinside/omymodels/tree/main/example\n\n## About library\n\nO! My Models (omymodels) is a library that allow you to **generate** different ORM \u0026 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).\n\nSupported Models:\n\n- SQLAlchemy (https://docs.sqlalchemy.org/en/14/orm/), \n- SQLAlchemy Core (Tables) (https://docs.sqlalchemy.org/en/14/core/metadata.html#accessing-tables-and-columns),\n- GinoORM (https://python-gino.org/), \n- Pydantic (https://pydantic-docs.helpmanual.io/),\n- Python Enum (https://docs.python.org/3/library/enum.html) - generated only from DDL SQL Types,\n- Python Dataclasses (dataclasses module) (https://docs.python.org/3/library/dataclasses.html),\n\n\n## How to install\n\n```bash\n\n    pip install omymodels\n\n```\n\n\n## How to use\n\n### From Python code\n### Create Models from DDL\n\nBy default method **create_models** generate GinoORM models, to get Pydantic models output use the argument `models_type='pydantic'` ('sqlalchemy' for SQLAlchemy models; 'dataclass' for Dataclasses; 'sqlalchemy_core' for Sqlalchemy Core Tables).\n\nA lot of examples in tests/ - https://github.com/xnuinside/omymodels/tree/main/tests.\n\nFor example,\n\n```python\nfrom omymodels import create_models\n\n\nddl = \"\"\"\nCREATE table user_history (\n     runid                 decimal(21) null\n    ,job_id                decimal(21)  null\n    ,id                    varchar(100) not null\n    ,user              varchar(100) not null\n    ,status                varchar(10) not null\n    ,event_time            timestamp not null default now()\n    ,comment           varchar(1000) not null default 'none'\n    ) ;\n\n\n\"\"\"\nresult = create_models(ddl, models_type='pydantic')['code']\n\n # and output will be:    \nimport datetime\nfrom typing import Optional\nfrom pydantic import BaseModel\n\n\nclass UserHistory(BaseModel):\n\n    runid: Optional[int]\n    job_id: Optional[int]\n    id: str\n    user: str\n    status: str\n    event_time: datetime.datetime\n    comment: str\n\n```\n\nTo generate Dataclasses from DDL use argument `models_type='dataclass'`\n\nfor example:\n\n```python\n    #  (same DDL as in Pydantic sample)\n    result = create_models(ddl, schema_global=False, models_type='dataclass')['code']\n\n    # and result will be: \n    import datetime\n    from dataclasses import dataclass\n\n\n    @dataclass\n    class UserHistory:\n\n        id: str\n        user: str\n        status: str\n        runid: int = None\n        job_id: int = None\n        event_time: datetime.datetime = datetime.datetime.now()\n        comment: str = 'none'\n```\n\n\nGinoORM example. If you provide an input like:\n\n```sql\n\nCREATE TABLE \"users\" (\n  \"id\" SERIAL PRIMARY KEY,\n  \"name\" varchar,\n  \"created_at\" timestamp,\n  \"updated_at\" timestamp,\n  \"country_code\" int,\n  \"default_language\" int\n);\n\nCREATE TABLE \"languages\" (\n  \"id\" int PRIMARY KEY,\n  \"code\" varchar(2) NOT NULL,\n  \"name\" varchar NOT NULL\n);\n\n\n```\n\nand you will get output:\n\n```python\n\n    from gino import Gino\n\n\n    db = Gino()\n\n\n    class Users(db.Model):\n\n        __tablename__ = 'users'\n\n        id = db.Column(db.Integer(), autoincrement=True, primary_key=True)\n        name = db.Column(db.String())\n        created_at = db.Column(db.TIMESTAMP())\n        updated_at = db.Column(db.TIMESTAMP())\n        country_code = db.Column(db.Integer())\n        default_language = db.Column(db.Integer())\n\n\n    class Languages(db.Model):\n\n        __tablename__ = 'languages'\n\n        id = db.Column(db.Integer(), primary_key=True)\n        code = db.Column(db.String(2))\n        name = db.Column(db.String())\n\n\n```\n\n#### From cli\n\n```bash\n\n    omm path/to/your.ddl\n\n    # for example\n    omm tests/test_two_tables.sql\n\n```\n\nYou can define target path where to save models with **-t**, **--target** flag:\n\n```bash\n\n    # for example\n    omm tests/test_two_tables.sql -t test_path/test_models.py\n\n```\n\nIf you want generate the Pydantic or Dataclasses models - just use flag **-m** or **--models_type='pydantic'** / **--models_type='dataclass'**\n\n```bash\n\n    omm /path/to/your.ddl -m dataclass\n\n    # or \n    omm /path/to/your.ddl --models_type pydantic\n\n```\n\nSmall library is used for parse DDL- https://github.com/xnuinside/simple-ddl-parser.\n\n\n### What to do if types not supported in O!MyModels and you cannot wait until PR will be approved\n\nFirst of all, to parse types correct from DDL to models - they must be in types mypping, for Gino it exitst in this file:\n\nomymodels/gino/types.py  **types_mapping**\n\nIf you need to use fast type that not exist in mapping - just do a path before call code with types_mapping.update()\n\nfor example:\n\n```python\n\n    from omymodels.models.gino import types\n    from omymodels import create_models\n\n    types.types_mapping.update({'your_type_from_ddl': 'db.TypeInGino'})\n\n    ddl = \"YOUR DDL with your custom your_type_from_ddl\"\n\n    models = create_models(ddl)\n\n    #### And similar for Pydantic types\n\n    from omymodels.models.pydantic import types  types_mapping\n    from omymodels import create_models\n\n    types.types_mapping.update({'your_type_from_ddl': 'db.TypeInGino'})\n\n    ddl = \"YOUR DDL with your custom your_type_from_ddl\"\n\n    models = create_models(ddl, models_type='pydantic')\n```\n\n### Schema defenition\n\nThere is 2 ways how to define schema in Models:\n\n1) Globally in Gino() class and it will be like this:\n\n```python\n\n    from gino import Gino\n    db = Gino(schema=\"schema_name\")\n\n```\n\nAnd this is a default way for put schema during generation - it takes first schema in tables and use it. \n\n2) But if you work with tables in different schemas, you need to define schema in each model in table_args. O!MyModels can do this also. Just use flag `--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:\n\n```python\n\n    ddl = \"\"\"\n    CREATE TABLE \"prefix--schema-name\".\"table\" (\n    _id uuid PRIMARY KEY,\n    one_more_id int\n    );\n        create unique index table_pk on \"prefix--schema-name\".\"table\" (one_more_id) ;\n        create index table_ix2 on \"prefix--schema-name\".\"table\" (_id) ;\n    \"\"\"\n    result = create_models(ddl, schema_global=False)\n```\n\nAnd result will be this:\n\n``` python\n\n    from sqlalchemy.dialects.postgresql import UUID\n    from sqlalchemy.schema import UniqueConstraint\n    from sqlalchemy import Index\n    from gino import Gino\n\n    db = Gino()\n\n\n    class Table(db.Model):\n\n        __tablename__ = 'table'\n\n        _id = db.Column(UUID, primary_key=True)\n        one_more_id = db.Column(db.Integer())\n\n        __table_args__ = (\n                    \n        UniqueConstraint(one_more_id, name='table_pk'),\n        Index('table_ix2', _id),\n        dict(schema=\"prefix--schema-name\")\n                )\n```\n\n## TODO in next releases\n\n1. Add Sequence generation in Models (Gino, SQLAlchemy)\n2. Add support for pure Python Classes (https://docs.python.org/3/tutorial/classes.html#class-objects)\n3. Add support for Tortoise ORM (https://tortoise-orm.readthedocs.io/en/latest/),\n4. Add support for DjangoORM Models\n5. Add support for Pydal Models\n6. Add support for Encode/orm Models\n\n\n## How to contribute\nPlease describe issue that you want to solve and open the PR, I will review it as soon as possible.\n\nAny questions? Ping me in Telegram: https://t.me/xnuinside or mail xnuinside@gmail.com\n\nIf you see any bugs or have any suggestions - feel free to open the issue. Any help will be appritiated.\n\n## Appretiation \u0026 thanks\n\nOne more time, big 'thank you!' goes to https://github.com/archongum for Web-version: https://archon-omymodels-online.hf.space/ \n\n## Changelog\n**v0.17.0**\n\n### Updates\n1. fix character varying type - https://github.com/xnuinside/omymodels/issues/59\n2. sqlalchemy import removed from generation in sqlmodels if it is not used\n3.  = Field() - is not placed in SQLModel if there is no defaults or other settings to the field\n\n**v0.16.0**\n\n### Updates\n1. Initial SQLModel Support\n\n\n**v0.15.1**\n## Updates\n1. Foreign Key processing updates - https://github.com/xnuinside/omymodels/pull/55\n2. Move to simple-ddl-parser version 1.X\n\n**v0.14.0**\n## Updates\n\n1. Python 3.11 support. \n\n\n**v0.13.0**\n## New feature\n\n1. Added argument 'schema_global=' to support SQLAlchemy \u0026 Gino different table schemas https://github.com/xnuinside/omymodels/issues/41\n\n**v0.12.1**\n### Improvements\n\n1. current_timestamp function processed now same was as \"now()\" function from ddl\n\n\n**v0.12.0**\n### Fixes\n1. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35\n\n### New feature:\n1. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36. \nNow, if you want to keep names 1-to-1 as in your DDL file, you can set argument `no_auto_snake_case=True` and O!MyModels will do nothing with the table or column names.\n\n\n\n**v0.11.1**\n\n### Improvements:\n1. added bytes type to pydantic - https://github.com/xnuinside/omymodels/pull/31\n2. parser version updated to the latest \n\n\n**v0.11.0**\n\n### Fixes:\n\n1. MSSQL column \u0026 tables names in [] now is parsed validly  - https://github.com/xnuinside/omymodels/issues/28\n2. names like 'users_WorkSchedule' now converted correctly to PascalCase like UsersWorkSchedule\n\n\n**v0.10.1**\n1. Update simple-ddl-parser version to 0.21.2\n\n\n**v0.10.0**\n### Improvements:\n1. Meta models moved to separate package - https://github.com/xnuinside/table-meta\n2. `common` module renamed to `from_ddl`, but anyway please use public API as imports from main module:\n\n`from omymodels import create_models` or `from omymodels import convert_models`\n\n### Fixes:\n\n1. Fixed bunch of bugs in converter, but it stil in 'beta'.\n2. Previously you can generate models if was any tables in ddl. Now you can also generate Enum models if in ddl you have only CREATE TYPE statements.\n3. String enums now in any models types will be inherit from (str, Enum)\n\n\n### Features:\n\n1. Added converter feature to convert one model type to another (excluding SQLAlchemy Core (Tables)). \nNow with more tests for supported models, but still in Beta with bucnh of issues.\n\n**v0.9.0**\nFeatures:\n1. Added beta models converter from one type of models to another.\nTo use models convertor:\n\n```python\nfrom omymodels import convert_models\n\n\nmodels_from = \"\"\"\n\nclass MaterialType(str, Enum):\n\n    article = \"article\"\n    video = \"video\"\n\n\n@dataclass\nclass Material:\n\n    id: int\n    title: str\n    description: str\n    link: str\n    type: MaterialType\n    additional_properties: Union[dict, list]\n    created_at: datetime.datetime\n    updated_at: datetime.datetime\n\n\"\"\"\n\nresult = convert_models(models_from, models_type=\"gino\")\nprint(result)\n```\n\nwhere `models_type` - type of models that you want to get as a result\n\n2. Now if O!MyModels does not know how to convert type - he just leave it as is.\n\nFixes:\n1. In Dataclass \u0026 Pydantic generators now Decimals \u0026 Floats converted to float (previously was int).\n\n**v0.8.4**\n1. Now if tables was not found in input DDL - models generator raise NoTable error. if you want to have still silent exit if no tables, please use flag: exit_silent\n\n**v0.8.3**\n1. Added fundamental concept of TableMetaModel - class that unifies metadata parsed from different classes/ORM models types/DDLs to one standard to allow easy way convert one models to another\nin next releases it will be used for converter from one type of models to another.\n2. Fixed issue: https://github.com/xnuinside/omymodels/issues/18 \"NOW() not recognized as now()\"\n3. Fixed issue: https://github.com/xnuinside/omymodels/issues/19 \"Default value of now() always returns same time, use field for dataclass\"\n\n**v0.8.1**\n1. Parser version is updated (fixed several issues with generation)\n2. Fixed issue with Unique Constraint after schema in SQLAlchemy Core\n\n**v0.8.0**\n1. Fix --defaults-off flag in cli\n2. Added support for SQLAlchemy Core Tables generating\n3. Added examples folder in github `omymodels/example`\n4. Fix issue with ForeignKey in SQLAlchemy\n\n**v0.7.0**\n1. Added generation for SQLAlchemy models (defaults from DDLs are setting up as 'server_default')\n2. Added defaults for Pydantic models\n3. Added flag to generate Pydantic \u0026 Dataclass models WITHOUT defaults `defaults_off=True` (by default it is False). And cli flag --defaults-off\n4. Fixed issue with Enum types with lower case names in DDLs\n5. Fixed several issues with Dataclass generation (default with datetime \u0026 Enums)\n6. '\"' do not remove from defaults now\n\n**v0.6.0**\n1. O!MyModels now also can generate python Dataclass from DDL. Use argument models_type='dataclass' or if you use the cli flag --models_type dataclass or -m dataclass\n2. Added ForeignKey generation to GinoORM Models, added support for ondelete and onupdate\n\n**v0.5.0**\n1. Added Enums/IntEnums types for Gino \u0026 Pydantic\n2. Added UUID type\n3. Added key `schema_global` in create_models method (by default schema_global = True). \nIf you set schema_global=False schema if it exists in ddl will be defined for each table (model) in table args.\nThis way you can have differen schemas per model (table). By default schema_global=True - this mean for all \ntable only one schema and it is defined in `db = Gino(schema=\"prefix--schema-name\")`.\n4. If column is a primary key (primary_key=True) nullable argument not showed, because primary keys always are not null.\n5. To cli was added flag '--no-global-schema' to set schema in table_args.\n\n**v0.4.1**\n1. Added correct work with table names contains multiple '-'\n\n**v0.4.0**\n1. Added generation for Pydantic models from ddl\n2. Main method create_gino_models renamed to create_models\n\n**v0.3.0**\n1. Generated Index for 'index' statement in __table_args__ (not unique constrait as previously)\n2. Fix issue with column size as tuple (4,2)\n\n**v0.2.0**\n1. Valid generating columns in models: autoincrement, default, type, arrays, unique, primary key and etc.\n2. Added creating __table_args__ for indexes\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fomymodels","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxnuinside%2Fomymodels","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fomymodels/lists"}