tags:

views:

538

answers:

3

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?

A: 

How does your definition of the log-records look? Do you get the same error if you create a new table from scratch (i.e. remove the Mnesia@ directory first).

JesperE
+2  A: 

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.

Haldun
+5  A: 

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.

Adam Lindberg