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

Q. Pluck fields not serializing in nested relationship #626

Open
noeunkim opened this issue Jan 3, 2025 · 0 comments
Open

Q. Pluck fields not serializing in nested relationship #626

noeunkim opened this issue Jan 3, 2025 · 0 comments

Comments

@noeunkim
Copy link

noeunkim commented Jan 3, 2025

I got an issue where Pluck fields are not serializing correctly in a nested relationship.
This is an example related with my question:

I found screw_size and screw_total are serializing correctly, but test_screw_size and test_screw_total (which use Pluck) are not being included in the serialized output.
Is there a way to make Pluck work in this scenario without modifying the query?

# Model and Schema
Class Robot(Base):
    __tablename__ = "robot"
    __table_args__ = {"schema": "test"}
    id = Column(Integer, primary_key=True)
    age = Column(Integer)
    size = Column(Integer)
    total = Column(Integer)
    ...
    nail = relationship("Nail", primaryjoin="Robot.part == foreign(Nail.part)")

Class Nail(Base):
    __tablename__ = "nail"
    __table_args__ = {"schema": "test"}
    size = Column(Integer, primary_key=True)
    total = Column(Integer)
    ...
    screw = relationship("Screw", primaryjoin="Nail.id == foreign(Screw.id)")

Class Screw(Base):
    __tablename__ = "screw"
    __table_args__ = {"schema": "test"}
    size = Column(Integer, primary_key=True)
    total = Column(Integer)
    ...

class NailSchema(BaseSchema):
    """Schema for Nail Model"""

    _model = Nail
    screw_size = fields.Str(attribute="screw.size")
    screw_total = fields.Str(attribute="screw.total")
    
    # TODO: need to solve, why this is not working?
    test_screw_size = fields.Pluck(ScrewSchema, "size")
    test_screw_total = fields.Pluck(ScrewSchema, "total")

class ScrewSchema(BaseSchema):
    """Schema for Screw Model"""

    _model = Screw

class RobotSchema(BaseSchema):
    """Schema for Robot Model"""

    _model = Robot
    nail_part = fields.Method("get_nail")

    @staticmethod
    def get_nail(obj: Robot) -> dict | None:
        return NailSchema(many=True).dump(obj.nail)

# Query
robots = session.query(Nail).options(joinedload(Nail.screw))
nails = (
    session.query(Robot.id, Robot.age)
    .options(joinedload(Nail.screw)
)
nail_alias = aliased(Nail, nails)
robot_alias = aliased(Roboy, robots)

robots_with_nail = (
    session.query(robot_alias)
    .outerjoin(nail_alias, nail_alias.size == robot_alias.size)
    .options(contains_eager(robot_alias.nail))
)
test_result = RobotSchema(many=True).dump(robots_with_nail)
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

No branches or pull requests

1 participant