-
Notifications
You must be signed in to change notification settings - Fork 66
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
base: main
Are you sure you want to change the base?
Conversation
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
for more information, see https://pre-commit.ci
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) |
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
There was a problem hiding this 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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 💯
There was a problem hiding this comment.
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
@jowilf |
This PR improves how primary key fields are handled in the SQLAlchemy model converter, addressing both field ordering and inheritance support.
Changes:
Primary Key Field Ordering
Polymorphic Inheritance Support
These changes ensure a more robust and consistent handling of primary keys across both simple and inherited model structures.