Add virtual field to Ship
schema
field(:number, :integer, virtual: true)
And use the following query:
ships_indices_query =
from s in Ship, select: %{s | number: row_number() |> over(order_by: s.id)}
query =
from si in subquery(ships_indices_query), where: si.id == ^id