the table identifier does not refer to an existing ETS table

201 Views Asked by At

In a GenServer

  def init(_opts \\ []) do
    table = :ets.new(:my_table, [:duplicate_bag, :public])
    {:ok, %{}}
  end

  def add_player(zone_id, socket_id) do
    :ets.thing(
      :my_table,
      {some_key, [my_uuid]}
    )
  end

But I get the table identifier does not refer to an existing ETS table

It's a public table, and I know init is getting called before the test executes because I logged in there.

I'm not sure why ets is doing this.

1

There are 1 best solutions below

1
legoscia On

In order to reference the table by its name, you need to make it a named table when creating it:

    table = :ets.new(:my_table, [:duplicate_bag, :public, :named_table])

Without :named_table, you need to use the reference returned by :ets.new to access the table.

(Also note that the table will be deleted if the process that created it dies, but that doesn't seem to be what's happening here.)