Skip to content

Basic CRUD

This guide shows the simplest table lifecycle: register a model, create schema, insert a row, query it, update it, count it, and delete it.

What The Example Covers

  • Ormdantic(...) database creation.
  • @db.table(...) model registration.
  • await db.init() schema creation.
  • db[Model] table handles.
  • insert, find_one, find_many, update, count, and delete.
"""Basic Ormdantic CRUD example."""

import asyncio

from pydantic import BaseModel

from ormdantic import Ormdantic

db = Ormdantic("sqlite:///examples_basic_crud.sqlite3")


@db.table(pk="id", indexed=["name"])
class Flavor(BaseModel):
    id: str
    name: str
    strength: int


async def main() -> None:
    await db.init()
    await db.drop_all()
    await db.create_all()

    mocha = Flavor(id="1", name="mocha", strength=5)
    await db[Flavor].insert(mocha)

    found = await db[Flavor].find_one("1")
    assert found == mocha

    mocha.strength = 6
    await db[Flavor].update(mocha)

    assert await db[Flavor].count() == 1
    await db[Flavor].delete("1")


if __name__ == "__main__":
    asyncio.run(main())

Run it locally:

python examples/basic_crud.py

Production Notes

Use db.init() for new local databases and tests. For production schema changes, use migrations so you can inspect generated SQL and keep migration history.