Problem with "expected at least one result but got none in query"

I learning phoenix(1.3.4) by making Link Bookmarking for myself, i use it for almost a month and there is no problem with functionality. 3 days ago i updated Elixir & Erlang version and my application still fine after recompile with lot of warnings. But last night when i tried to open the profile page suddenly i got “expected at least one result but got none in query”, and I haven’t touched the application code in a 2 weeks.

Ecto.NoResultsError at GET /@1
expected at least one result but got none in query:
from l in Eltar.Topic.Link,
  where: l.id == ^"1",
  preload: [:user]

lib/ecto/repo/queryable.ex
def one!(repo, adapter, queryable, opts) do
    case all(repo, adapter, queryable, opts) do
      [one] -> one
 -->  []    -> raise Ecto.NoResultsError, queryable: queryable
      other -> raise Ecto.MultipleResultsError, queryable: queryable, count: length(other)
    end
  end

DB access code:

 def get_link_with_user(id) do
    Link
    |> preload(:user)
    |> Repo.get!(id)
  end

Controller code:

  def profile(conn,%{"id" => id}) do
    link = Topic.get_link_with_user(id)
    render(conn, "profile.html", page_title: link.user.login <> " - Eltar",link: link)
  end

Router:
get "/@:id", LinkController, :profile

Template:

<a href="/@<%= link.user.id %>" >
          <%= link.user.login %>
</a> <%= link.inserted_at |> seconds_ago() |> time_ago_in_words() %>

How can i fix this error?

Hi there, seems to me that the link with id 1 doesn’t exist in the database :slight_smile: (Repo.get!(id) in the db access code, I know the id is one from the error code).

So check the database if that link exists. Otherwise the link seems to be a string and not a number, I remember both working interchangeably but I might be wrong :slight_smile:

In short what you need to get working is Repo.get!(Link, id) as it seems to return no result.

Thank you for your replay…
When i turn off debug_errors its really return no result. But i tried with copy-paste

 def get_link_with_user(id) do
    Link
    |> preload(:user)
    |> Repo.get!(id)
  end

then rename to get_profile(id) and then i apply to profile controller, now the profile page is working again.
Thank you very much :smile: