naymspace / backpex

Backpex is a highly customizable administration panel for Phoenix LiveView applications.
https://backpex.live
MIT License
360 stars 20 forks source link

Crash after creating a resource #416

Closed lukaszsamson closed 1 week ago

lukaszsamson commented 2 weeks ago

backpex version: 0.4.0 phoenix version: 1.7.14 phoenix_live_view version: 0.20.17 elixir version: 1.17.1 otp version: 26.2.5

I followed the installation guide on a freshly generated mix phoenix project. I applied the fixes from https://github.com/naymspace/backpex/issues/399 and got the app to boot but it crashes when I try to create a post

[error] ** (ArgumentError) attempting to set id attribute to 1, but setting the DOM ID to a number can lead to unpredictable behaviour. Instead consider prefixing the id with a string, such as "user-1" or similar
    (phoenix_html 4.1.1) lib/phoenix_html.ex:258: Phoenix.HTML.id_value/1
    (phoenix_html 4.1.1) lib/phoenix_html.ex:223: Phoenix.HTML.build_attrs/1
    (phoenix_html 4.1.1) lib/phoenix_html.ex:191: Phoenix.HTML.attributes_escape/1
    (backpex 0.4.0) lib/backpex/html/resource/resource_index_table.html.heex:78: anonymous fn/4 in Backpex.HTML.Resource."resource_index_table (overridable 1)"/1
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:391: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:559: anonymous fn/3 in Phoenix.LiveView.Diff.traverse_comprehension/5
    (elixir 1.17.1) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (elixir 1.17.1) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (elixir 1.17.1) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:480: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:532: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
    (elixir 1.17.1) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:389: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:532: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
    (elixir 1.17.1) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:389: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:532: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
    (elixir 1.17.1) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:389: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 0.20.17) lib/phoenix_live_view/diff.ex:532: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7

Here's my post schema

defmodule BackpexPlayground.Blog.Post do
  use Ecto.Schema
  import Ecto.Changeset

  schema "blog_posts" do
    field :title, :string
    field :views, :integer

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(post, attrs) do
    post
    |> cast(attrs, [:title, :views])
    |> validate_required([:title, :views])
  end

  def update_changeset(post, attrs, _metadata \\ []) do
    changeset(post, attrs)
  end

  def create_changeset(post, attrs, _metadata \\ []) do
    changeset(post, attrs)
  end
end

and live view

defmodule BackpexPlaygroundWeb.Live.PostLive do
  use Backpex.LiveResource,
    layout: {BackpexPlaygroundWeb.Layouts, :admin},
    schema: BackpexPlayground.Blog.Post,
    repo: BackpexPlayground.Repo,
    update_changeset: &BackpexPlayground.Blog.Post.update_changeset/3,
    create_changeset: &BackpexPlayground.Blog.Post.create_changeset/3,
    pubsub: BackpexPlayground.PubSub,
    topic: "posts",
    event_prefix: "post_"

  @impl Backpex.LiveResource
  def singular_name, do: "Post"

  @impl Backpex.LiveResource
  def plural_name, do: "Posts"

  @impl Backpex.LiveResource
  def fields do
  [
    title: %{
      module: Backpex.Fields.Text,
      label: "Title"
    },
    views: %{
      module: Backpex.Fields.Number,
      label: "Views"
    }
  ]
  end
end
nedhgx commented 2 weeks ago

The problem is caused by the posts table's primary key type being bigserial. If you change it to binary_id, it should be fine!

Flo0807 commented 2 weeks ago

The problem is caused by the posts table's primary key type being bigserial. If you change it to binary_id, it should be fine!

Exactly. We will support integer id types with the next version. The PR is already merged (see #366).

Flo0807 commented 1 week ago

Hey, we relased 0.5 with support for integer id types. If you still have issues, feel free to create another issue.

lukaszsamson commented 1 week ago

I tested master branch earlier and the crash is fixed