When creating mnesia, you must specify the table record structure. If the defined record structure is modified, you must update the table structure of the data. Otherwise, mnesia cannot read and write data normally.
We initially defined the structure in this way.
-Record (person, {name, age }).
Then, create a table.
Mnesia: create_table (person, [{disc_only_copies, nodes ()}, {attributes, record_info (fields, person)}]).
One day, we changed the person structure.
-Record (person, {name, age, money }).
We can also read mnesia data, but it cannot match the current person structure.
> Mnesia: dirty_read (person, Lucy ).
[{Person, Lucy, 1}]
And we cannot write any more data.
> Mnesia: dirty_write (person, # person {name = Lily, age = 2, money = 0 }).
** Exception Exit: {aborted, {bad_type, # person {name = Lily, age = 2, money = 0 }}}
In function mnesia: Abort/1 (mnesia. erl, line 309)
But we still want to use this database, so we can modify the database.
Fun = fun (x)->
Case X
{Person, name, age}->
{Person, name, age, 0 };
_->
X
End
End,
Newattr = [name, age, money],
Mnesia: transform_table (person, fun, newattr, person ).
Refer:
Http://blog.csdn.net/mycwq/article/details/30101659