Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mnesia write fails

Tags:

erlang

mnesia

I defined a record named log. I want to create an mnesia table with name log_table. When I try to write a record to table, I get bad_type error as follows:

(node1@kitt)4> mnesia:create_table(log_table, [{ram_copies, [node()]}, 
                                               {attributes, record_info(fields, log)}]).
{atomic,ok}

(node1@kitt)5> mnesia:dirty_write(log_table, #log{id="hebelek"}).
** exception exit: {aborted,{bad_type,#log{id = "hebelek"}}}
in function  mnesia:abort/1

What am I missing?

like image 752
Haldun Avatar asked Jan 24 '23 02:01

Haldun


2 Answers

By default the record name is assumed to be the same as the table name.

To fix this you should either name your table just log or append the option {record_name, log} in your table options (as you've done in your fix).

It is usually good practice to let your record and table be named the same thing, it makes the code easier to read and debug. You can also then use the mnesia:write/1 function with just your record as only argument. Mnesia then figures out which table to put the record in by looking at the name.

like image 65
Adam Lindberg Avatar answered Feb 04 '23 07:02

Adam Lindberg


I've found it. When I changed mnesia:create_table call to this

mnesia:create_table(log_table, [{ram_copies, [node()]},
                                {record_name, log},
                                {attributes, record_info(fields, log)}]).

everything works OK.

like image 29
Haldun Avatar answered Feb 04 '23 07:02

Haldun