igorbenav / fastcrud

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities.
MIT License
530 stars 32 forks source link

Cannot use get or get_multi filter with like string #101

Closed Justinianus2001 closed 2 weeks ago

Justinianus2001 commented 3 weeks ago

Hello,

I'm currently utilizing FastCRUD with the FastAPI boilerplate, but I'm unsure how to implement a filter to retrieve a string from a field similar to the SQL 'LIKE' query, as shown below:

count_projects = await crud_projects.count(db=db, project_code=f"{short_code}%")

I need to count the projects whose project_code begins with a specific short_code (for example, "ABC"). Could you assist me in resolving this issue?

Thank you!

igorbenav commented 3 weeks ago

PR #85 will fix this, it's almost done

Justinianus2001 commented 3 weeks ago

While waiting for the resolution of the previous issue, I have another question regarding nested join query formats.

I aim to retrieve a project and its nested objects based on client_id, department_id, and assignee_id as follows:

db_project = await crud_projects.get_joined(
    db=db, id=id, is_deleted=False, nest_joins=True, schema_to_select=ProjectRead,
    joins_config=[
        JoinConfig(
            model=Client,
            join_on=Project.client_id == Client.id,
            join_prefix="client",
            schema_to_select=ClientRead,
            join_type="left",
        ),
        JoinConfig(
            model=Department,
            join_on=Project.department_id == Department.id,
            join_prefix="department",
            schema_to_select=DepartmentRead,
            join_type="left",
        ),
        JoinConfig(
            model=User,
            join_on=Project.assignee_id == User.id,
            join_prefix="assignee",
            schema_to_select=UserReadSub,
            join_type="left",
        ),
    ],
)

However, I encounter an issue: when a foreign key is null, the object is still returned but with all fields set to None.

{
    'id': 1,
    'project_code': 'ABC1',
    'status': 'Approved',
    'name': 'Project Example',
    'start_date': datetime.datetime(2024, 6, 5, 9, 26, 48, 914000, tzinfo=datetime.timezone.utc),
    'end_date': datetime.datetime(2024, 6, 5, 9, 26, 48, 914000, tzinfo=datetime.timezone.utc),
    'client': {
        'name': None,
        'contact': None,
        'phone': None,
        'email': None,
        'id': None
    },
    'department': {
        'id': 1,
        'name': 'Department Name'
    },
    'assignee': {
        'id': None,
        'name': None,
        'username': None,
        'email': None,
        'phone': None,
        'profile_image_url': None,
        'department_id': None,
        'company_id': None
    }
}

How can I modify the query so that if a nested object's foreign key is null, the entire nested object is returned as None (e.g., 'client': None)?

Thank you.

igorbenav commented 3 weeks ago

This is a bug, I'll fix it!

igorbenav commented 2 weeks ago

Closed by #85