我有这张表:
-record(person, {id, firstname, lastname}).
-record(person_backup, {id, firstname, lastname}).
我用:
创建表person_backup create_backup()
mnesia:create_table(person_backup,[{disc_copies, [node()]},{attributes, record_info(fields, person)},
{record_name, person}]).
我也有这个功能:
create_table_increment()->
mnesia:create_table(my_auto_inc,[{type,set}]).
我想通过一些修改将数据从人传输到person_backup:
例如,如果表人有:
13 asma chabani
14 nawel jirard
15 ahme bahri
表person_backup成为:
1 asma chabani
2 nawel jirard
3 ahme bahri
我尝试使用此代码:
test()->
Match=#person{_ = '_'}, %Will match all records
Fun = fun() ->
List = mnesia:match_object(Match),
lists:foreach(fun(X) ->
NextKey = mnesia:dirty_update_counter(my_auto_inc,lastrec,1),
Update = X#person_backup{id=NextKey,firstname = #person.firstname,lastname=#person.lastname},
mnesia:write(Update)
end, List)
end,
mnesia:transaction(Fun).
但是当我测试时我有这个错误:
1> model:test().
{aborted,{combine_error,my_auto_inc,update_counter}}
我也尝试使用此代码:
TEST2() - >
Match=#person{_ = '_'}, %Will match all records
Fun = fun() ->
List = mnesia:match_object(Match),
lists:foreach(fun(X) ->
NextKey = mnesia:dirty_update_counter(my_auto_inc,lastrec,1),
Update = X#person_backup{id=NextKey,firstname = #person.firstname,lastname=#person.lastname},
mnesia:write(Update)
end, List)
end,
mnesia:transaction(Fun).
并显示此错误:
model:test2().
{aborted,{{badrecord,person_backup},
[{model,'-test2/0-fun-0-',1},
{lists,foreach,2},
{mnesia_tm,apply_fun,3},
{mnesia_tm,execute_transaction,5},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_exprs,6},
{shell,eval_loop,3}]}}
答案 0 :(得分:1)
您已使用记录人创建了person_backup表,在写入时,您正在使用person_backup记录,这会导致错误。