Erlang, creating mnesia table index

70 Views Asked by At

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}

2

There are 2 best solutions below

0
Gabor Szelei On BEST ANSWER

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.

0
7stud On

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

By default, it's already an index: the first element of a record is the primary key.