Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Model logo

Model

A minimal Python ORM for MariaDB/MySQL and SQLite. Explicit, predictable, and made for humans.

Motivation

Many ORMs try to abstract SQL away entirely, introducing their own query languages and complex concepts that force you to spend time learning “their way” before getting productive.

Model takes a different approach: it embraces native type definitions and SQL instead of hiding them behind unnecessary abstractions.

Features

  • Intuitive and simple to use.
  • Advanced static type checking for fool-proof schema definition. (Fun fact: the Column method has 73 typing @overloads.)
  • Fast and predictable performance, with no hidden bloated queries slowing down your app.
  • One command for automatic schema updates. Safe, rapid and versionless schema evolution.
  • One command to automatically generate models from your existing tables.
  • Primarily written by hand.
Example usage.

Example usage of Model CLI.

Installation

python -m pip install model-py

Note: You import the package as model

Documentation

Read the documentation at https://model.elis.cc

Quickstart

1. Define a model

A model represents a database table. You just need to define the database instance, table name and columns.

Create example file: ./models/user.py

from model import Model
from model.database import SQLiteDatabase

db = SQLiteDatabase("./example.db") # this could be MySQLDatabase

class User(Model):
    table = "user"
    db = db

    id: int = Model.Column(
        type="INT",
        index="PRIMARY",
        auto_increment=True,
    )
    email: str = Model.Column(
        type="VARCHAR",
        length=255,
        index="UNIQUE",
        can_be_null=False,
    )
    age: int | None = Model.Column(type="INT")

2. Configure model discovery

Create model.config.yaml in the project root:

include_dirs:
  - ./models

3. Create the table

The model sync CLI is the easiest way to 'sync' models with the database. It automatically generates SQL diffs based on your model definitions. To keep it safe, there are some restrictions in place for column deletion and renaming.

Run CLI commands from your project root so Model can find the configuration.

Preview the generated schema change:

model sync check

If the SQL looks correct, apply it:

model sync apply

Run model sync check once more. It should report nothing left to apply.

That's it!


Now we can use the model

Model instances always represent persisted database records. Loading, inserting, updating, and querying are explicit operations, so it's easy to understand exactly what your code is doing - no complex object lifecycle, no ambiguous save().

Insert

insert() creates the row and returns a loaded model instance.

from models.user import User

user = User.insert({
    "email": "john.doe@example.com",
    "age": 30
})

print(user.id, user.email)
# prints: 1, john.doe@example.com

Load by the primary key

Constructing a model loads an existing row.

The primary key is automatically inferred for the model class initialization:

Automatic initialization column inferring

from models.user import User

user = User(id=1)

print(user.id, user.email)
# prints: 1, john.doe@example.com

It raises ModelRecordNotFoundError when no row matches.

Query

Find records with SQL conditions. SQLite uses ? for parameter placeholders (while MySQL uses %s):

from models.user import User

user = User.find_one("email = ?", ["john.doe@example.com"])
assert user is not None

print(user.id, user.email) # prints: 1, john.doe@example.com

all_users = User.find_all("age > 24 ORDER BY age")
count = User.count("email LIKE ?", ["%@example.com"])

print(all_users) # prints: [User(...)] 
print(count) # prints: 1

find_one() returns None when there is no match, while find_all() returns an empty list.

Update

Records are updated explicitly; assign new values through update().

from models.user import User

user = User(id=1)

print(user.email)
# prints: john.doe@example.com

user.update({
    "email": "johnny@example.com"
})

print(user.email)
# prints: johnny@example.com

Delete

user.delete()

This deletes the row in the database. After deletion, that instance can no longer be used for record operations.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

model_py-1.0.0rc1.tar.gz (84.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

model_py-1.0.0rc1-py3-none-any.whl (98.4 kB view details)

Uploaded Python 3

File details

Details for the file model_py-1.0.0rc1.tar.gz.

File metadata

  • Download URL: model_py-1.0.0rc1.tar.gz
  • Upload date:
  • Size: 84.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.4

File hashes

Hashes for model_py-1.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 baa7e0063e39c127b771ba23a74356f7fb7abed82c4165f6085907c52bee5619
MD5 20b4dcfda4ca5a8d7167df427e0daa20
BLAKE2b-256 25fa5b0273195b625b6085a0cea3cf71508280283f89de38d6d19cc49141e1b7

See more details on using hashes here.

File details

Details for the file model_py-1.0.0rc1-py3-none-any.whl.

File metadata

  • Download URL: model_py-1.0.0rc1-py3-none-any.whl
  • Upload date:
  • Size: 98.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.4

File hashes

Hashes for model_py-1.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a4595e931e7088a955a79939b15ec9e67525e5d00b25e2e5ff7c162144ed080
MD5 8f7f0d7675f9f017aec1063fd214bd8f
BLAKE2b-256 53d5d6657ecc29ffc86985beb6aa8f81ea937ca20d49c5045e0ee9140e34883b

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.0

2 files

This release

1.0.0rc1 This release

2 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