Creating Mnesia table returns {:aborted, {:bad_type, ... }}

Hi,
I keep getting this error when attempting to create a Mnesia table:
{:bad_type, Prev_store, {:attributes, [:prev]}}

Please, how do I solve this? Here’s the code:

defmodule SData do
  alias :mnesia, as: Mnesia
  @node  Node.self()

  def create() do
    Mnesia.create_schema([@node])

    Mnesia.start()

    case Mnesia.create_table(Prev_store, [attributes: [:prev], disc_copies: [@node]]) do
      {:atomic, :ok} -> ["table_created"]
      {:aborted, err_msg} -> IO.inspect(err_msg)
    end
  end
end

@node Node.self() this is not a good idea. This value is set at compile time, but your code is run at runtime. I don’t know if this is the specific issue you have, but you should definitely just call Node.self() at runtime and not store it in a module attribute.

Hey, thanks for the advice.

Replacing the args with node() doesn’t solve the problem: ...create_schema([node()]) and ...[attributes: [:prev], disc_copies: [node()]] even after deleting the database and starting afresh.

Is ‘Prev_store’ an atom?

Yes Jey, in Elixir :atom and Atom are equivalent.

They are by no mean equivalent. Even more, :Atom and Atom are not equivalent. The former is just an atom as it’s written, the latter is expanded to :"Elixir.Atom" by compiler.

3 Likes

:wave:

From the mnesia docs:

The table must at least have one extra attribute in addition to the key.

The key in your case seems to be :prev, so you need one more attribute.

Try something like

defmodule SData do
  alias :mnesia, as: Mnesia

  def create() do
    Mnesia.create_schema([Node.self()])

    Mnesia.start()

    case Mnesia.create_table(Prev_store, [attributes: [:prev, :another_key], disc_copies: [Node.self()]]) do
      {:atomic, :ok} -> ["table_created"]
      {:aborted, err_msg} -> IO.inspect(err_msg)
    end
  end
end
3 Likes

@idiot That works, thanks :+1:

Yeah, both types will work because they are both atoms but they are technically inequivalent.
Thanks for putting that straight. :slightly_smiling_face: