Search code examples
erlangmnesia

How to create a mnesia table with one column & read/write from/to there?


-record(ng, {ng}).

mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).

I'm getting: {aborted,{bad_type,ng,{attributes,[ng]}}} error.

What's wrong? How to create a mnesia table with one column(which is named)?


Solution

  • The record has to have at least 2 fields. This would work:

    -record(ng, {ng, extrafield}).
    mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).
    

    From http://www.erlang.org/doc/man/mnesia.html#create_table-2

    "The table must have at least one extra attribute in addition to the key."

    Edit: Can't find an answer as to whether a single column is possible, but this 2007 thread indicates not.

    I personally do it with key/value columns, like this:

    -record(proximaglobal, {key, value}).
    mnesia:create_table(proximaglobal, [{attributes, record_info(fields, proximaglobal)}, {disc_only_copies, [node()]}]).
    mnesia:sync_transaction(fun() -> mnesia:write(#proximaglobal{key=time, value=WorldTime}) end).
    mnesia:sync_transaction(fun() -> mnesia:read(proximaglobal, time) end).