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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement