Mnesia exception exit: {aborted,{bad_type, Record}}
Asked Answered
D

1

5

I create a table whose name is NOT the same as its record name. Here below is the code snippet

%% ---- record definition --------------------------------
-record(object,{key,value}).
%% ---- create table ------------------------------------- {atomic,ok} = mnesia:create_table(mytable,[ {type,set}, {frag_properties,[ {node_pool,[node()]}, {n_fragments,4}, {n_disc_copies,1}]}, {attributes,record_info(fields,object)}] ),

%% ------- inserting --------------------------------
insert()-> F = fun() -> R = #object{ key = "MyKey", value = "Value" }, mnesia:write(mytable,R,write) end, mnesia:activity(transaction,F,[],mnesia_frag).
On doing this, mnesia cries out loud with an execption. The table is created very well, and can be viewed in tv:start(). or in mnesia:info().. This is the error i see on my shell.
** exception exit: {aborted,
                       {bad_type,
                           #object{
                               key = "MyKey",
                               value = "Value"}}}
     in function  mnesia:wrap_trans/6 (mnesia.erl, line 395)
Now, normally, i thought that one would get such an error when the record definition used when creating a table is different from the record structure being inserted in the table. I wonder is this is just a problem with the function i am using, that is: mnesia:write/3 which helps when the table name is different from the record name.

I have tried deleting the schema and creating it anew, but all in vain. When i do not use mnesia:write/3, the record gets inserted properly in the table. But my application needs are such that i will have several different tables which are created but they store the same record structure/definition. I want to have different tables but their record_info definition is the same.

Somewhere in the docs, i read that this is very possible. I am running: Erlang otp R15B, mnesia-4.6 , windows 7 enterprise, 32-bit operating system, Dell laptop, intel core i5, 4GB RAM All other erlang projects i am working on are fine, they have no wierd/unexpected behavior like this one has.
Any suggestions ?
Dysphasia answered 16/5, 2012 at 15:30 Comment(0)
A
12

You must use the {record_name, object} property when creating the table. e.g.

{atomic,ok} = mnesia:create_table(mytable,[
                {type,set},
                {frag_properties,[
                            {node_pool,[node()]},
                            {n_fragments,4},
                            {n_disc_copies,1}]},
                {record_name, object},
                {attributes,record_info(fields,object)}]
            ),

From the docs:

{record_name, Name}, where Name must be an atom. All records, stored in the table, must have this name as the first element. It defaults to the same name as the name of the table.

Airstrip answered 16/5, 2012 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.