Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61061

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

Upvotes: 3

Views: 1593

Answers (2)

GuessBurger
GuessBurger

Reputation: 478

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.

Upvotes: 6

Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61061

Phew! Thanks to this excellent blog post to leading me to an answer, quote:

This error:

{aborted,{bad_type,wrud_record,{index,[2]}}}

will occur if you used the first element of the record to index one table, like:

-record(wrud_record, {user, date, label, remark, url}).

and

mnesia:create_table( wrud_record,[ {index,[user]}, {attributes, record_info(fields, wrud_record)}])

, so you should change the index to another element like remark here:

mnesia:create_table( wrud_record,[ {index,[remark]}, {attributes, record_info(fields, wrud_record)}])

everything will be fine. :)

Upvotes: 2

Related Questions