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(#164): added mapper column key on to_dict func #165

Merged
merged 3 commits into from
May 3, 2024

Conversation

ysnbyzli
Copy link
Contributor

@ysnbyzli ysnbyzli commented Apr 26, 2024

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR
  • Pre-Commit Checks were ran and passed
  • Tests were ran and passed

Description

When I want to avoid using the column names found in the SQL table, as in the ExampleModel below, I create custom column names to correspond to them. However, when I try to perform an upsert operation on the table's service, instead of getting the custom column names I provided to the model in the to_dict function, it retrieves the column names from the SQL table. Hence, it does not match the column names in the model.

We've encountered this issue before with the model_from_dict function, and we fixed it by adding the following code. #78

Model:

class ExampleModel(BigIntAuditBase):
    __tablename__ = "Example_Model"

    field_one: Mapped[str] = mapped_column(
        "FIELDONE", String(10), ForeignKey("ERP_Items.erp_code"), nullable=False
    )
    field_two: Mapped[str] = mapped_column(
        "FIELDTWO", String(10), ForeignKey("ERP_STAR_ITEMS.erp_code"), nullable=False
    )

Current:

    def to_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
        """Convert model to dictionary.

        Returns:
            dict[str, Any]: A dict representation of the model
        """
        exclude = {"sa_orm_sentinel", "_sentinel"}.union(self._sa_instance_state.unloaded).union(exclude or [])  # type: ignore[attr-defined]
        return {field.name: getattr(self, field.name) for field in self.__table__.columns if field.name not in exclude}

Must be:

def to_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
        """Convert model to dictionary.

        Returns:
            dict[str, Any]: A dict representation of the model
        """
        exclude = {"sa_orm_sentinel", "_sentinel"}.union(self._sa_instance_state.unloaded).union(exclude or [])  # type: ignore[attr-defined]
        return {field: getattr(self, field) for field in self.__mapper__.columns.keys() if field not in exclude}

Close Issue(s)

@ysnbyzli ysnbyzli requested review from a team as code owners April 26, 2024 13:31
Copy link

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

Copy link
Member

@cofin cofin left a comment

Choose a reason for hiding this comment

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

LGTM

@cofin cofin merged commit c09f717 into litestar-org:main May 3, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants