Skip to content

feat Custom table name configuration (TableConfig) #8

@Ox54

Description

@Ox54

Summary

The database table name used for a model should be configurable. If not explicitly defined, Ferro should continue to use the current sensible default (class name lowercased, e.g. Useruser).

Proposed API

Follow a pattern similar to Pydantic's ConfigDict: allow a table_config attribute on any model class.

from ferro import Model
from pydantic import ConfigDict

class User(Model):
    model_config = ConfigDict(...)   # Pydantic-related model config
    table_config = TableConfig(name="users", ...)  # Ferro table-related configuration
  • table_config should be turned into a class variable automatically by the metaclass (so users define it as a normal attribute; the metaclass ensures it is treated as configuration).
  • Default: When table_config is omitted or TableConfig.name is not set, use the current behavior: table name = cls.__name__.lower().

Implementation notes

  • Python: Introduce a TableConfig type (e.g. in base or a dedicated config module) with at least a name: str | None (or similar) for the table name. Metaclass should read table_config from the class namespace and pass the resolved table name into schema generation/registration.
  • Rust: The core already receives a "name" when registering the schema (register_model_schema(name, ...)). That name is lowercased and used as the table name everywhere. So the Python side only needs to pass the configured table name (or the default cls.__name__.lower()) as the registered name; the Rust API can remain name-based.
  • Migrations / relations: Any code that derives table name from the model (e.g. model_name.lower() in migrations, to_table in relations) should use the same resolved table name from table_config (or default) for consistency.

Acceptance criteria

  • TableConfig (or equivalent) exists with a name attribute for the table name.
  • Models can set table_config = TableConfig(name="users") and the table name used in SQL/migrations is the configured value.
  • When table_config is omitted or name is not set, behavior is unchanged (table name = class name lowercased).
  • table_config is established as a class variable by the metaclass so it is not treated as a field.
  • Docs and any relevant examples updated to show optional table name configuration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions