Search code examples
erlangmnesia

Mnesia write fails


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?


Solution

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