Search code examples
erlangmnesia

Erlang, creating mnesia table index


Why the first element of a record can't be used as table index?

This is not working:

([email protected])16> rd(pilot, {id, name, weight, phone}).
pilot
([email protected])17> mnesia:create_table(pilot,
([email protected])17> [{attributes, record_info(fields, pilot)},
([email protected])17> {index, [#pilot.id]},
([email protected])17> {disc_copies, Nodes},
([email protected])17> {type, set}]).
{aborted,{bad_type,pilot,{index,[{2,ordered}]}}}

This is working:

([email protected])18> rf(pilot).                                
ok
([email protected])19> rd(pilot, {name, weight, phone, id}).     
pilot
([email protected])20> mnesia:create_table(pilot,
([email protected])20> [{attributes, record_info(fields, pilot)},
([email protected])20> {index, [#pilot.id]},
([email protected])20> {disc_copies, Nodes},
([email protected])20> {type, set}]).
{atomic,ok}


Solution

  • I've found a single statement in the documentation:

    The first record attribute is the primary key, or key for short.

    Later on:

    index. This is a list of attribute names, or integers, which specify the tuple positions on which Mnesia is to build and maintain an extra index table.