[erlang-questions] Modifying an mnesia table key

Hakan Mattsson <>
Fri May 29 11:59:21 CEST 2009


On Fri, 29 May 2009, Bengt Kleberg wrote:

> Greetings,
> 
> If you want to change the keys for all entries in a mnesia table you
> have to loop over the whole table (either mnesia:foldl/3 or
> mnesia:first/1, mnesia:next/2) and
> 1) read the object
> 2) delete it
> 3) modify it
> 4) write the new object.
> 
> The way the code in mnesia_schema:transform_obj/9 is written makes it
> impossible to change the key. There is probably a good reason for this,
> but I do not understand why.

One reason is that the result may be unpredictable as it is dependent
of the order of the records in the table. If the transform fun would
be allowed to change the key of a record into a key that already is
used by another record, you would get different results depending on
the order of the keys. Your suggested solution above has the same
problem. You need to first read all keys, iterate over the stable set
of keys and keep track of potential key clashes so you can handle them
correctly.

/Håkan
---
Håkan Mattsson (uabhams)
Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list