aesmail / kaffy

Powerfully simple admin package for phoenix applications
https://kaffy.fly.dev/admin/
MIT License
1.3k stars 153 forks source link

Use left_join when building search query. #273

Closed aglassman closed 10 months ago

aglassman commented 1 year ago

This allows consistent search results when using preloads in a custom_index_query, and search_fields.

I ran into issues when trying to search my Profile schema by User.email. Changing to a left join allowed the search filter to work, and provided consistent results.

Schema Modules

defmodule MyApp.Profile do

  schema "profiles" do
    belongs_to :user, User
    field :display_name, :string
    ...
  end

end

defmodule MyApp.User do

  schema "users" do
    has_one :profile, Profile
    field :email, :string
    ...
  end

end

Admin Module

defmodule MyApp.ProfileAdmin

  def custom_index_query(_conn, _schema, query) do
    from(r in query, preload: [:user])
  end

  def search_fields(_schema) do
    [
      :display_name,
      user: [:email]
    ]
  end