Search code examples
erlangmnesiaerl

How to add a column to mnesia table


I am trying to add new column to an existing mnesia table. For that, I use following code.

test()->

Transformer =
  fun(X)->
      #users{name = X#user.name,
           age = X#user.age,
           email = X#user.email,
          year = 1990}
end,
{atomic, ok} = mnesia:transform_table(user, Transformer,record_info(fields, users),users).

Two records I have

-record(user,{name,age,email}).
-record(users,{name,age,email,year}).

My problem is when I get values from my user table it comes as

{atomic,[{users,sachith,28,sachith@so,1990}]}

Why do I get users record name when I retrieve data from user table?


Solution

  • The table name and the record name are not necessarily the same. You started out with a table called user holding user records, and then you transformed all the user records into users records. So when you read from the table, it will return users records, since that's what the table now contains.