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

Bug: or Feature? SQLAlchemyDTO ignores include/exclude config when generating field definitions #236

Open
1 of 4 tasks
bookin opened this issue Jul 23, 2024 · 0 comments
Open
1 of 4 tasks
Labels
bug Something isn't working

Comments

@bookin
Copy link

bookin commented Jul 23, 2024

Description

I've encountered an issue while using SQLAlchemyDTO with a SQLAlchemy model (without using Litestar's mixins). The DTO configuration to include only specific fields is being ignored, leading to unexpected behavior in my case.

class ReturnModelDTO(SQLAlchemyDTO[SomeModel]):
    config = SQLAlchemyDTOConfig(include={"id", "name"}, partial=False)

@get('/test', return_dto=ReturnModelDTO)
async def get_data(self) -> list[SomeModel]:
    ...

class SomeModel(DeclarativeBase, AsyncAttrs):
    __tablename__ = 'some_table'
    id = mapped_column(Integer, primary_key=True, autoincrement=True)
    name = mapped_column(String, nullable=False)
    topic_id = mapped_column(UUID, ForeignKey("topic.id"), nullable=False)
    topic: Mapped["Topic"] = relationship("Topic", lazy="joined")

I receive the following error:

raise NotImplementedError()

This error occurs in the following line:

return FieldDefinition.from_annotation(Optional[elem.type.python_type])

The error seems to be related to a field in the Topic model. However, I'm not sure why SQLAlchemyDTO is attempting to get field definitions for the relation when I've explicitly set the include config to only include "id" and "name".

Upon investigation, it appears that the generate_field_definitions(...) method in SQLAlchemyDTO doesn't utilize the include / exclude config keys. Instead, it generates definitions for all fields found in the model.

Is this behavior intended, or is it a bug?

Thank you for your time and assistance.

URL to code causing the issue

No response

MCVE

No response

Steps to reproduce

No response

Screenshots

No response

Logs

No response

Package Version

2.9.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
@bookin bookin added the bug Something isn't working label Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant