patric-vinicios

patric-vinicios

FLOP - Filter across table does not work as expected

Using the example from the lib itself, let’s suppose I have 10 owners and each owner has 10 pets.

@derive {
Flop.Schema,
filterable: [:pet_species],
sortable: [:pet_species],
adapter_opts: [
join_fields: [
pet_species: [
binding: :pets,
field: :species,
ecto_type: :string
]
]
]
}
schema “owners” do
field :name, :string
field :email, :string

has_many :pets, Pet
end

schema “pets” do
field :name, :string
field :species, :string

belongs_to :owner, Owner
end

After setting up the join fields, I can write a query like this:

params = %{
page_size: 10,
page: 1,
filters: [%{field: :pet_species, op: :==, value: “E. africanus”}]
}

Owner
|> join(:left, [o], p in assoc(o, :pets), as: :pets)
|> preload([pets: p], [pets: p])
|> Flop.validate_and_run!(params, for: Owner)

The expected result is that it would return 10 owners and all their associations, in this case, for each owner it would return 10 pets. But this query returns 1 owner and their 10 pets. If I sent apage_size: 5, it would return 1 owner and 5 pets.

This other query works the way I expect, returning 10 users and all their pets:

params = %{page_size: 10, page: 1, filters: [%{field: :pet_species, op: :==, value: “E. africanus”}]}

Owner
|> preload([o], pets: ^from(p in Pet))
|> limit(^page_size)
|> Flop.validate_and_run(params, for: Owner)

But this way, the filters are not applied.

How could I fix it?

Most Liked

fuelen

fuelen

Try to add distinct: o.id.
However, I wouldn’t use Flop for filtering in this case. I prefer writing queries myself and in this case it would use where + exists expression because usually it will be more performant and more convenient (no dances with ordering, for example) than join + distinct. Actually, Flop can use this approach under the hood, but I doubt.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement