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

values_list querying and returning wrong flattened data #1060

Open
jonfinerty opened this issue Apr 18, 2023 · 0 comments
Open

values_list querying and returning wrong flattened data #1060

jonfinerty opened this issue Apr 18, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@jonfinerty
Copy link

When using values_list through a relationship ormar is return all non-nullable fields and the taking the first one as the flattened value, which is the wrong field/values.

An example, say I have a list of car ids, and I want to find the set of owners for that set of cars. Each car has a garage, each garage has an owner. I only want the owner_id's for efficiency sake.

car_ids = [1, 2, 3]

await Cars.objects.select_related([Car.garage])
    .filter(id__in=car_ids)
    .values_list("garage__owner", flatten=True)

Car has a bunch of non-nullable fields as does Garage and Owner, hence why I want to use values_list to query raw data and not hydrate actual models

When this is executed I actually get the whole of the Car and Garage models queried for (I can see the SELECTs for these columns in the SQL), and then https://github.com/collerek/ormar/blob/master/ormar/queryset/queryset.py#L640 is just grabbing the first value, which happens to be the Car.id not anything to do with Car.garage.owner.id.

I've tried using fields_exclude to explicitly remove non-nullable fields but it doesn't work. I've tried a combination of flatten and exclude_through with similar results.

As far as I can tell, using values_list isn't allowing me to bypass non-nullable fields? Is there something I've missed, or not understood about querying raw data like this?

@jonfinerty jonfinerty added the bug Something isn't working label Apr 18, 2023
@jonfinerty jonfinerty changed the title Values_list querying and returning wrong flattened data values_list querying and returning wrong flattened data Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant