Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqla): Improve primary key handling in model conversion #629

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

noamsto
Copy link
Contributor

@noamsto noamsto commented Feb 18, 2025

This PR improves how primary key fields are handled in the SQLAlchemy model converter, addressing both field ordering and inheritance support.

Changes:

  1. Primary Key Field Ordering

    • Move primary key fields to the start of the converted fields list
    • Maintain consistent form layout with PKs at the top
    • Preserve ordering of non-PK fields
  2. Polymorphic Inheritance Support

    • Add support for inherited primary key columns
    • Bypass multiple-column property restriction for inherited PKs
    • Maintain existing validation for other column cases

These changes ensure a more robust and consistent handling of primary keys across both simple and inherited model structures.

noamsto and others added 3 commits February 18, 2025 22:01
Add support for polymorphic inheritance by allowing inherited primary key columns to be converted without the multiple-column property restriction.

Previously, the converter would reject inherited primary keys due to the multiple-column property check. This change allows these columns to bypass the restriction while maintaining it for other cases.
Modify the field conversion logic to ensure primary key fields appear first in the converted fields list. This maintains a consistent field order where primary keys are always at the start of the form.

- Move inherited primary key fields to start of list
- Place regular primary key fields at beginning of converted fields
- Keep non-primary key fields at their original positions
@noamsto noamsto marked this pull request as ready for review February 18, 2025 20:39
@noamsto
Copy link
Contributor Author

noamsto commented Feb 19, 2025

@jowilf

With this fix it's the following simple case is working:

from uuid import UUID, uuid4

from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship


class Base(DeclarativeBase):
    pass


class Product(Base):
    __tablename__ = "products"
    __mapper_args__ = {
        "polymorphic_identity": "product",
        "polymorphic_on": "_type",
    }

    _type: Mapped[str] = mapped_column(String(50))
    id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
    name: Mapped[str] = mapped_column(String(100))
    price: Mapped[int] = mapped_column(Integer())
    store_id: Mapped[UUID] = mapped_column(ForeignKey("stores.id"))
    store: Mapped[Store] = relationship("Store")


class Book(Product):
    __tablename__ = "books"
    __mapper_args__ = {
        "polymorphic_identity": "book",
        "polymorphic_on": "_type",
    }
    id: Mapped[UUID] = mapped_column(ForeignKey("products.id"), primary_key=True)


class Movie(Product):
    __tablename__ = "movies"
    __mapper_args__ = {
        "polymorphic_identity": "movie",
        "polymorphic_on": "_type",
    }
    id: Mapped[UUID] = mapped_column(ForeignKey("products.id"), primary_key=True)

Copy link

codecov bot commented Feb 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (2540c51) to head (89e4651).
Report is 33 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #629    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           75        76     +1     
  Lines         5817      5926   +109     
==========================================
+ Hits          5817      5926   +109     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, can you add a test case for this? Ideally something like

async def test_model_fields_conversion():

if is_inherited_pk:
column = attr.columns[0]
converted_fields.insert(
0,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's maintain the order in the model class definition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this doesn't have so much effect. Since you have to declare the PK in the inheriting model.
So 💯

Copy link
Contributor Author

@noamsto noamsto Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this doesn't have so much effect. Since you have to declare the PK in the inheriting model. So 💯

This is actually wrong... The test showed differently,
Consider these models:

class BaseItem(Base):
    __tablename__ = "base_items"
    __mapper_args__ = {
        "polymorphic_identity": "base_item",
        "polymorphic_on": "_type",
    }

    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    _type = Column(String(50))


class SpecialItem(BaseItem):
    __mapper_args__ = {"polymorphic_identity": "special_item"}
    special_power = Column(String(50))

Without the insert, the id primary key will be after the fields of SpecialItem, i.e.:
["special_power", "id", "name", "_type"]

And if there would be more fields it will be pushed back...
This is bad UX in my opinion, you can end up with the ID out of the screen

Add test cases to verify correct field conversion for SQLAlchemy polymorphic models:
- Add BaseItem and SpecialItem test models with polymorphic inheritance
- Add test_polymorphic function to verify field conversion for both base and child models
- Verify proper handling of inherited fields and type discriminator
@noamsto
Copy link
Contributor Author

noamsto commented Feb 19, 2025

@jowilf
I add a test, hope it is good enough.
Tell me if you want me to squash the commits into one

@noamsto noamsto requested a review from jowilf February 19, 2025 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants