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

Annotate related_object_count #50

Open
JakNowy opened this issue Apr 10, 2024 · 5 comments
Open

Annotate related_object_count #50

JakNowy opened this issue Apr 10, 2024 · 5 comments
Labels
enhancement New feature or request FastCRUD Methods Related to FastCRUD methods

Comments

@JakNowy
Copy link
Contributor

JakNowy commented Apr 10, 2024

Is your feature request related to a problem? Please describe.
I can't find a way to annotate number of related objects using the fastcrud.get_multi(). This is a raw sqlalchemy query I have instead: [note m2m relationship in this specific case]

    searches = await db.execute(
        select(Search, func.count(Video.id))
        .outerjoin(VideoSearchAssociation)
        .outerjoin(Video, VideoSearchAssociation.video_id == Video.id)
        .group_by(Search.id)
    )

Describe the solution you'd like
I'd like to be able to retrieve videos_count from search_crud.get_multi() method and potentialy others. Extending to_select with func.count(Video.id) seems relatively easy, but whole query must be wrapped in a GROUP_BY clause, which seems to be a bit harder to implement in an elegant way. Maybe some CountConfig resembling JoinConfig would be a way to go?

@JakNowy JakNowy added the enhancement New feature or request label Apr 10, 2024
@igorbenav igorbenav added the FastCRUD Methods Related to FastCRUD methods label Apr 12, 2024
@igorbenav
Copy link
Owner

Do you mean something like

video_count_config = CountConfig(
    related_model=Video,
    join_condition=(Video.id == VideoSearchAssociation.video_id) 
                   & (VideoSearchAssociation.search_id == Search.id),
    count_alias='videos_count'
)

Then passing it to a get_multi_joined, and getting something like:

{
    "data": [
        {
            "id": 1,
            "term": "cats",
            "videos_count": 5  // This Search has 5 associated Videos
        },
        {
            "id": 2,
            "term": "dogs",
            "videos_count": 3  // This Search has 3 associated Videos
        },
        {
            "id": 3,
            "term": "birds",
            "videos_count": 0  // This Search has no associated Videos
        }
    ],
    "total_count": 3  // Total number of Search records retrieved
}

?

@JakNowy
Copy link
Contributor Author

JakNowy commented Apr 15, 2024

Yep, I think something like that would be perfect. Looks pretty flexible and extendable. In particular supports m2m relationship. It should be safe to always groupby the primary_model.id (as it's required by sqlalchemy syntax).

@igorbenav
Copy link
Owner

I'm fixing this one next

@igorbenav
Copy link
Owner

Sorry, this is taking a while. I'm fixing all the other issues related to joined methods before adding a new feature

@JakNowy
Copy link
Contributor Author

JakNowy commented May 6, 2024

No worries, alot of new issues recently. Let's not force anything!

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