Search code examples
erlangmnesia

"bad_type" error when trying to create mnesia tables


For the life of me, can't figure this out. I'm trying to create mnesia tables, but keep getting this weird error.

Here is my command:

ok = mnesia:create_schema(Nodes),
rpc:multicall(Nodes, application, start, [mnesia]),
{_, ok} = mnesia:create_table(rr_events,
        [{attributes, record_info(fields, rr_events)},
         {index, [#rr_events.key]},
         {disc_copies, Nodes}]),
rpc:multicall(Nodes, application, stop, [mnesia]).

Here is my record:

-record(rr_events, {key, events=[]}).

Here is the error:

=PROGRESS REPORT==== 24-Mar-2016::21:53:42 ===
         application: mnesia
          started_at: nonode@nohost
** exception error: no match of right hand side value
                    {aborted,{bad_type,rr_events,{index,[2]}}}
     in function  rr:install/1 (c:/Users/zzzz/Projects/zzz/zzz/rr/rr/_build/default/lib/rr/src/rr.erl, line 13)

Any idea what this might be? Cannot figure this out.


Solution

  • I came across this issue recently. Learn you some Erlang states the following:

    Note that you do not need to put an index on the first field of the record, as this is done for you by default.

    If you only need to index on the first element of a record then I would advise omitting {index, [record_name]}.

    Also, while the paragraph from LYSE suggests it the official Erlang documentation goes one further and states:

    index. This is a list of attribute names, or integers, which specify the tuple positions on which Mnesia is to build and maintain an extra index table.