Skip to content

Conversation

@cofin
Copy link
Member

@cofin cofin commented Nov 16, 2025

Summary

This PR completes the implementation of SQLAlchemy inheritance pattern support in CommonTableAttributes, enabling proper handling of Single Table Inheritance (STI), Joined Table Inheritance (JTI), and Concrete Table Inheritance (CTI).

Changes

Core Implementation (advanced_alchemy/base.py)

  1. Added __init_subclass__ hook (lines 209-277):

    • Detects STI children by checking if any parent has polymorphic_on in their __mapper_args__
    • Automatically sets cls.__tablename__ = None for STI children
    • Handles both explicit (with polymorphic_identity) and implicit (without) STI children
    • Properly distinguishes between base classes and child classes
  2. Enhanced @declared_attr.__tablename__() method (lines 281-386):

    • Returns None for explicitly set None values (set by __init_subclass__)
    • Provides fallback STI detection for cases where parent doesn't have explicit tablename
    • Generates snake_case table names for non-STI classes

Supported Patterns

  1. Single Table Inheritance (STI):

    • Child classes with polymorphic_identity share parent's table
    • Works with both auto-generated and explicit parent table names
    • Handles edge case of children without polymorphic_identity (with warning)
  2. Joined Table Inheritance (JTI):

    • Child classes with explicit __tablename__ create joined tables
    • Proper foreign key relationships maintained
  3. Concrete Table Inheritance (CTI):

    • Child classes with concrete=True create independent tables
    • No foreign keys to parent table

Related Issues

Supersedes PR #600

- Modified CommonTableAttributes.__tablename__ to detect inheritance patterns
- Changed from @declared_attr.directive (was causing issues) back to .directive
- Returns None for STI children to use parent's table
- Returns generated/explicit names for JTI/CTI patterns
- Handles edge cases: mixins, abstract bases, multi-level hierarchies

Fixes automatic table name generation for SQLAlchemy inheritance:
- Single Table Inheritance (STI): Child uses parent table
- Joined Table Inheritance (JTI): Child has own table with FK
- Concrete Table Inheritance (CTI): Independent tables with concrete=True
@cofin cofin force-pushed the fix/sqlalchemy-inheritance-proper branch from 7badcce to 863a70b Compare November 16, 2025 22:48
@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/advanced-alchemy-docs-preview/611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant