How does mnesia modify the table structure?

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.