Skip to content

Getting Started

This guide walks you through installing Lythonic and creating your first database schema.

Installation

Install with pip:

pip install lythonic

Or with uv:

uv add lythonic

Define Your First Model

Create a Python file with your schema definition:

from pydantic import Field
from lythonic.state import DbModel, Schema

class Task(DbModel["Task"]):
    task_id: int = Field(default=-1, description="(PK)")
    title: str = Field(description="Task title")
    completed: bool = Field(default=False)

SCHEMA = Schema([Task])

Key points:

  • Inherit from DbModel["YourClassName"] (the string must match the class name)
  • Mark primary keys with (PK) in the field description
  • Use default=-1 for auto-increment primary keys

Create the Database

from pathlib import Path

SCHEMA.create_schema(Path("tasks.db"))

This generates and executes:

CREATE TABLE Task (
    task_id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    completed INTEGER NOT NULL
)

Insert and Query Data

from lythonic.state import open_sqlite_db

with open_sqlite_db("tasks.db") as conn:
    # Create a task
    task = Task(title="Learn Lythonic")
    task.save(conn)  # Inserts and sets task_id

    print(f"Created task with id: {task.task_id}")

    # Query all tasks
    all_tasks = Task.select(conn)

    # Query with filter
    incomplete = Task.select(conn, completed=False)

    # Load by ID
    loaded = Task.load_by_id(conn, task.task_id)

    conn.commit()

Next Steps