Tables

Define typehinted tables in Python as the source of truth for what your tables should look like within your database. These are used by both the typehint signatures, runtime, and the migration logic to enforce your schemas are as you define here.

Tables

CLASSiceaxe.base.TableBase

Base class for all database table models. Provides the foundation for defining database tables using Python classes with type hints and field definitions.

Features:

  • Automatic table name generation from class name
  • Support for custom table names
  • Tracking of modified fields for efficient updates
  • Support for unique constraints and indexes
  • Integration with Pydantic for validation

Class Attributes

  • Name
    model_fields
    Type
    dict[str, DBFieldInfo]
    Description
  • Name
    table_name
    Type
    str
    Description

    Optional custom name for the table

  • Name
    table_args
    Type
    list[UniqueConstraint | IndexConstraint]
    Description

    Table constraints and indexes

  • Name
    modified_attrs
    Type
    dict[str, Any]
    Description

    Dictionary of modified field values since instantiation or the last clear_modified_attributes() call. Used to construct differential update queries.

  • Name
    modified_attrs_callbacks
    Type
    list[Callable[[Self], None]]
    Description

    List of callbacks to be called when the model is modified.

Class Methods

  • Name
    get_modified_attributes
    Return type
    dict[str, Any]
    Description

    Get the dictionary of attributes that have been modified since instantiation or the last clear_modified_attributes() call.

  • Name
    clear_modified_attributes
    Return type
    None
    Description

    Clear the tracking of modified attributes. Typically called after successfully saving changes to the database.

  • Name
    get_table_name
    Return type
    str
    Description

    Get the table name for this model. Uses the custom table_name if set, otherwise converts the class name to lowercase.

  • Name
    get_client_fields
    Return type
    dict[str, DBFieldInfo]
    Description

    Get all fields that should be exposed to clients. Excludes internal fields used for model functionality.

  • Name
    register_modified_callback
    Return type
    None
    Description

    Register a callback to be called when the model is modified.

Code

class User(TableBase): # Custom table name (optional) table_name = "users" # Fields with types and constraints id: int = Field(primary_key=True) email: str = Field(unique=True) name: str is_active: bool = Field(default=True) # Table-level constraints table_args = [ UniqueConstraint(columns=["email"]), IndexConstraint(columns=["name"]) ] # Usage in queries query = select(User).where(User.is_active == True) users = await conn.execute(query)

Constraints

CLASSiceaxe.base.IndexConstraint

Represents an INDEX on one or more columns in a database table. Improves query performance for the specified columns.

Class Attributes

  • Name
    columns
    Type
    list[str]
    Description

    List of column names to create an index on

Code

class User(TableBase): email: str last_login: datetime table_args = [ IndexConstraint(columns=["last_login"]) ]

CLASSiceaxe.base.UniqueConstraint

Represents a UNIQUE constraint in a database table. Ensures that the specified combination of columns contains unique values across all rows.

Class Attributes

  • Name
    columns
    Type
    list[str]
    Description

    List of column names that should have unique values

Code

class User(TableBase): email: str tenant_id: int table_args = [ UniqueConstraint(columns=["email", "tenant_id"]) ]