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.
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.
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.
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