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}).On doing this, mnesia cries out loud with an execption. The table is created very well, and can be viewed in
%% ---- 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).
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.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. 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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With