Search code examples
erlangmnesia

Erlang Mnesia not working with size one record


I have been trying to make Mnesia work with records of size one, where the key is the value itself, but it does not seem to work.

Console:

Eshell V7.3  (abort with ^G)
1> c(mnesiac).
{ok,mnesiac}
2> mnesiac:in
init/0    insert/1  
2> mnesiac:init().
{aborted,{bad_type,player,{attributes,[id]}}}

Code:

  1 -module(mnesiac).
  2 -compile(export_all).
  3 
  4 -record(player, {id}).
  5 
  6 init() ->
  7     mnesia:create_schema([node()]),
  8     mnesia:start(),
  9     mnesia:create_table(player,
 10         [ {disc_copies, [node()] },
 11              {attributes,
 12                 record_info(fields,player)} ]).
 13 
 14 insert(Id) ->
 15     Fun = fun() ->
 16         mnesia:write( #player{id=Id})
 17     end,
 18     mnesia:transaction(Fun).

Can anyone point me in the right direction? (As another problem, we could only make mnesia work with records of 3 or more fields in another program, so I'm thinking there is something wrong somewhere)


Solution

  • It's not possible to store a record with only one field in a Mnesia table. That is mentioned in the documentation, though perhaps not where you would look first, but in the description of mnesia:create_table/2:

    • {attributes, AtomList} is a list of the attribute names for the records that are supposed to populate the table. Default is [key, val]. The table must at least have one extra attribute in addition to the key.

    (emphasis mine)