From 0157accf97777819e03778f5a9e4d6387f7e257f Mon Sep 17 00:00:00 2001 From: T Floyd Wright Date: Mon, 1 Jun 2026 21:53:32 +0300 Subject: [PATCH] fix: return nil from Resource.find on uncastable id Visiting a show/edit URL with an id that can't be cast to the schema's primary key type (e.g. "null" or "abc" for an integer pk) raised Ecto.Query.CastError during connected mount, surfaced as a 400. Rescue the cast error and return nil so the existing "No record found" path renders instead. --- lib/live_admin/resource.ex | 2 ++ test/live_admin/components/container_test.exs | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/live_admin/resource.ex b/lib/live_admin/resource.ex index 9ac5a9f..6d848c4 100644 --- a/lib/live_admin/resource.ex +++ b/lib/live_admin/resource.ex @@ -55,6 +55,8 @@ defmodule LiveAdmin.Resource do resource |> query(nil, config) |> repo.get(key, prefix: prefix) + rescue + Ecto.Query.CastError -> nil end def delete(record, resource, session, repo, config) do diff --git a/test/live_admin/components/container_test.exs b/test/live_admin/components/container_test.exs index d354c6e..ba85295 100644 --- a/test/live_admin/components/container_test.exs +++ b/test/live_admin/components/container_test.exs @@ -219,6 +219,18 @@ defmodule LiveAdmin.Components.ContainerTest do end end + describe "edit resource with id that cannot be cast" do + setup %{conn: conn} do + {:ok, view, _} = live(conn, "/user/edit/not-a-number") + + %{view: view} + end + + test "renders no record found message", %{view: view} do + assert has_element?(view, "*", "No record found") + end + end + describe "edit resource with embed" do setup %{conn: conn} do user = Repo.insert!(%User{settings: %{}})