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

Cannot use get or get_multi filter with like string #101

Closed
Justinianus2001 opened this issue Jun 4, 2024 · 4 comments
Closed

Cannot use get or get_multi filter with like string #101

Justinianus2001 opened this issue Jun 4, 2024 · 4 comments
Labels
enhancement New feature or request FastCRUD Methods Related to FastCRUD methods

Comments

@Justinianus2001
Copy link

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
Copy link
Owner

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

@igorbenav igorbenav added enhancement New feature or request FastCRUD Methods Related to FastCRUD methods labels Jun 4, 2024
@Justinianus2001
Copy link
Author

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
Copy link
Owner

This is a bug, I'll fix it!

@igorbenav
Copy link
Owner

Closed by #85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request FastCRUD Methods Related to FastCRUD methods
Projects
None yet
Development

No branches or pull requests

2 participants