Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"bad_type" error when trying to create mnesia tables

Tags:

erlang

mnesia

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.

like image 577
Andriy Drozdyuk Avatar asked Apr 14 '26 10:04

Andriy Drozdyuk


2 Answers

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.

like image 55
GuessBurger Avatar answered Apr 16 '26 01:04

GuessBurger


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

like image 27
Andriy Drozdyuk Avatar answered Apr 16 '26 02:04

Andriy Drozdyuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!