Skip to content

return_columns=True for FastCRUD.update(...) errors in TypeError: 'bool' object is not iterable #316

@UnoYakshi

Description

@UnoYakshi

Description
I was working on a patch to allow polymorphic inheritance. I stumbled upon fastcrud.crud.fast_crud::FastCRUD.update(...) with return_columns=True.

Inside of update(), there is a call of fastcrud.crud.execution.execute_update_and_return_response() that doesn't handle return_columns=True:

    if return_columns:
        stmt = stmt.returning(*[column(name) for name in return_columns])
        db_row = await db.execute(stmt)
        if commit:
            await db.commit()

        if allow_multiple:
            multi_data = [dict(row) for row in db_row.mappings()]
            formatted_data = format_multi_response(
                multi_data, schema_to_select, return_as_model
            )
            return {"data": formatted_data}

and I get an error:

>           stmt = stmt.returning(*[column(name) for name in return_columns])
                                                             ^^^^^^^^^^^^^^
E           TypeError: 'bool' object is not iterable

However, update()'s docstring says

return_columns: A list of column names to return after the update. If `return_as_model` is True, all columns are returned.

To Reproduce

import pytest

from fastcrud import FastCRUD
from pydantic import BaseModel

# conftest.py...
class Entity(Base):
    __tablename__ = 'entities'
    __mapper_args__ = {
        'polymorphic_on': 'type',
        'polymorphic_identity': 'entity',
        'with_polymorphic': '*',
    }
    id = Column(Integer, primary_key=True, autoincrement=True)
    type = Column(String(100), nullable=True, default='entity')


class UserEntity(Entity):
    __tablename__ = 'users_entities'
    __mapper_args__ = {
        'polymorphic_identity': 'user_entity',
    }
    id = Column(ForeignKey('entities.id'), primary_key=True)
    name = Column(String(100), nullable=True)


class UserEntityCreateSchema(BaseModel):
    name: str


# SQLAlchemy test...
@pytest.mark.asyncio
async def test_update_polymorphic(async_session):

    user = UserEntity(name="User A")
    async_session.add(user)
    await async_session.commit()
    await async_session.refresh(user)

    new_user_name = "Update User Name"
    new_raw_data = UserEntityCreateSchema(name=new_user_name)

    crud = FastCRUD(model=UserEntity)
    fetched_record = await crud.update(
        db=async_session,
        object=new_raw_data,
        return_as_db_obj=True,
        return_columns=True,
        id=user.id,
    )

    assert fetched_record is not None
    assert fetched_record.id == 1
    assert fetched_record.name == new_user_name
    assert fetched_record.type == "user_entity"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions