Get the last table entry submitted using Repo.one

I want to get the last table entry submitted using Repo.one

I have a simple Liveview that I created without the generator and I have another collection of non-liveview data and routes that I did create using a generator. The generated table is called Testbeds (all code below). I want to get the last table entry submitted to Testbeds and I am trying to use Repo.one to do this. I could do it on the Liveview client but I am curious what I am doing wrong and why Repo.one is not working. I am using the function Testbed.get_last()

Code

defmodule App.Testbeds do
@moduledoc β€œβ€"
The Testbeds context.
β€œβ€"


  import Ecto.Query, warn: false
  alias App.Repo

  alias App.Testbeds.Testbed
 
 # data ....
  def get_last() do 
      Repo.one(from x in App.Testbeds, order_by: [desc: x.id], limit: 1)

  end
end

Liveview

defmodule AppWeb.PageLive do
   use AppWeb, :live_view  
   alias App.Testbeds
   def mount(_params, _session, socket)  do

	 {:ok, assign(socket, testbed: Testbeds.get_last())} 
   end

   def render(assigns) do
	   ~H"""
	
	      <%= assigns.testbed.name%>

	     """ 

   end

end

Error

function App.Testbeds.__schema__/1 is undefined or private

1 Like

The error is quite clear: the module you’re referring to in the query is not a schema. You mixed up the context module and the schema module. Try ... from x in App.Testbeds.Testbed, order_by: ... (or use the shorter alias you have defined in there).

2 Likes