mnesia delete_object异常?

时间:2009-03-16 13:07:26

标签: erlang mnesia

我不明白这里有什么问题,但我可能会误解语法。我正在尝试从我的“用户”mnesia表中删除一个名为“ryan”的“用户”记录(有多个)。这是我的工作:

Delete=#user{first_name = "ryan", _ = '_'},
mnesia:dirty_delete_object(Delete)

这是我的记录定义:

-record(user, {id,
               username,
               password,
               email,
               first_name,
               last_name,
               last_login, % Date/time user last logged in
               reg_date}). % Date/time user registered the account

这是我得到的例外:

** exception exit: {aborted,
                       {bad_type,user,
                           {user,'_','_','_','_',"ryan",'_','_','_'}}}
     in function  mnesia:abort/1
     in call from users:register/1

这似乎是一场完美的比赛。什么可能导致这个问题?

1 个答案:

答案 0 :(得分:12)

mnesia:delete_object/1/3用于从表格中删除给定记录。它不会根据匹配规范删除多个记录。它只是mnesia:delete/*的更精确版本 - 它为您提供了一种从表中删除特定记录的方法。

要删除多个符合规范的记录(例如,使用name="ryan"),请使用以下代码:

Delete=#user{first_name = "ryan", _ = '_'},
List = mnesia:dirty_match_object(Delete)
lists:foreach(fun(X) ->
                      mnesia:dirty_delete_object(X)
              end, List).

或它的交易版本:

Delete=#user{first_name = "ryan", _ = '_'},
Fun = fun() ->
              List = mnesia:match_object(Delete)
              lists:foreach(fun(X) ->
                                    mnesia:delete_object(X)
                            end, List)
      end,
mnesia:transaction(Fun).