Welcome to your guide on using O! My Models, a powerful library that allows developers to generate various ORM models from SQL DDL statements. In essence, this library is akin to a magical translator that converts your database schema into Python code, making it easy for you to interact with databases.
Getting Started
To begin, you need to install the library. Let’s simply break that down!
Installation
bash
pip install omymodels
How to Use O! My Models
This library is versatile, and you can generate ORM models in different formats such as SQLAlchemy, Pydantic, GinoORM, and Python Dataclasses. Let’s explore how to create models from DDL statements.
Generating Models from SQL DDL
Imagine you’re a chef, and the DDL statement is a recipe. Each ingredient (table definition) allows you to create a delicious dish (Python model). Here’s how it works:
from omymodels import create_models
ddl = """
CREATE TABLE user_history (
runid decimal(21) null,
job_id decimal(21) null,
id varchar(100) not null,
user varchar(100) not null,
status varchar(10) not null,
event_time timestamp not null default now(),
comment varchar(1000) not null default none
);
"""
result = create_models(ddl, models_type=pydantic)
In this example, the chef takes his recipe written in DDL and translates it into delicious Pydantic models in a matter of seconds! The resulting model would look like this:
import datetime
from typing import Optional
from pydantic import BaseModel
class UserHistory(BaseModel):
runid: Optional[int]
job_id: Optional[int]
id: str
user: str
status: str
event_time: datetime.datetime
comment: str
Each ingredient in the recipe has been transformed into a Python attribute, making it ready for you to use in your application.
Using the Command Line
If you prefer to work from a command line, you can call the script directly by using the omm
command along with your DDL file:
omm pathtoyour.ddl
You can also define a target path to save the models. The library is highly configurable!
Troubleshooting
While using the O! My Models library, you may encounter some hiccups. Here are a few common issues and their solutions:
- Issue: Unsupported types in DDL.
- Solution: Add your custom type into the
types_mapping
dictionary before generating models:
from omymodels.models.gino import types
types.types_mapping.update({'your_type_from_ddl': db.TypeInGino})
If you need further insights, updates, or want to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.