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

HasManyThrough to Resource with a BelongsTo Fails #596

Open
geofflane opened this issue Sep 10, 2024 · 1 comment
Open

HasManyThrough to Resource with a BelongsTo Fails #596

geofflane opened this issue Sep 10, 2024 · 1 comment

Comments

@geofflane
Copy link

Describe the bug
Trying to create a HasManyThrough field to a resource that has a BelongsTo and get an error:

no match of right hand side value: nil

lib/backpex/fields/belongs_to.ex

  @impl Phoenix.LiveComponent
  def update(assigns, socket) do
    %{schema: schema, name: name, field: field} = assigns

    %{queryable: queryable, owner_key: owner_key} = schema.__schema__(:association, name)

    display_field = display_field(field)
    display_field_form = display_field_form(field, display_field)

The assigns is getting the schema for the Root resource and not for the "Through" resource.

Example Schemas and Config

defmodule MyApp.Report do
  use Ecto.Schema
  alias MyApp.ReportColumn
  alias MyApp.ReportColumnCalculation
  alias MyApp.ReportConstraint

  schema "reports" do
    field :key, :string
    field :title, :string
    field :description, :string

    has_many :report_columns, ReportColumn
    has_many :columns, through: [:report_columns, :column]

    timestamps()
  end
end


defmodule MyApp.ReportColumn do
  use Ecto.Schema
  alias MyApp.Column
  alias MyApp.Report

  schema "report_columns" do
    belongs_to :report, Report
    belongs_to :column, Column
    field :sort, :integer
    field :visible, :boolean, default: true

    timestamps()
  end
end


defmodule MyApp.Column do
  use Ecto.Schema
  alias MyApp.ColumnConstraint
  alias MyApp.Operation
  alias MyApp.QueryColumn
  alias MyApp.ReportColumn

  schema "columns" do
    field :title, :string
    field :abbreviation, :string
    field :description, :string

    field :format, Ecto.Enum,
      values: [
        regular: "regular",
        text: "text",
        float: "float",
        integer: "integer",
        percent: "percent"
      ]

    belongs_to :operation, Operation

    timestamps()
  end
end

defmodule MyApp.Operation do
  use Ecto.Schema

  schema "operations" do
    field :key, :string

    timestamps()
  end
end

defmodule MyAppWeb.Admin.ReportLive do
  use Backpex.LiveResource,
    #...

  @impl Backpex.LiveResource
  def fields do
    [
      title: %{
        module: Backpex.Fields.Text,
        label: "Title"
      },
      description: %{
        module: Backpex.Fields.Text,
        label: "Description"
      },
      columns: %{
        module: Backpex.Fields.HasManyThrough,
        label: "Columns",
        display_field: :title,
        live_resource: MyAop.Admin.ColumnLive,
        except: [:index],
        sort_by: [:sort],
        pivot_fields: [
          sort: %{
            module: Backpex.Fields.Number,
            label: "Sort"
          },
          visible: %{
            module: Backpex.Fields.Boolean,
            label: "Visible"
          }
        ]
      }
    ]
  end
end


defmodule MyAppWeb.Admin.ColumnLive do
  use Backpex.LiveResource,
    #...


  @impl Backpex.LiveResource
  def fields do
    [
      output_key: %{
        module: Backpex.Fields.Text,
        label: "Key"
      },
      title: %{
        module: Backpex.Fields.Text,
        label: "Title"
      },
      abbreviation: %{
        module: Backpex.Fields.Text,
        label: "Abbreviation"
      },
      description: %{
        module: Backpex.Fields.Text,
        label: "Description"
      },
      operation: %{
        module: Backpex.Fields.BelongsTo,
        label: "Operation",
        display_field: :key
      },
      format: %{
        module: Backpex.Fields.Text,
        label: "Format"
      }
    ]
  end
end

Expected behavior
I'd exepect this case to work and display the values from the "Through" resource.

@pehbehbeh
Copy link
Member

The HasManyThrough field is currently still in beta stage and not really easy to use / configure. It also has some bugs. We are already working on a new version which is basically a rewrite under the hood.

There is a (somewhat) working example in the demo - maybe this will help you in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants