2

我看不出这里有什么问题,但我可能只是误解了语法。我正在尝试从我的“用户”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

这似乎是一个完美的匹配。什么可能导致这个问题?

4

1 回答 1

12

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

要删除与规范匹配的多个记录(例如 with 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).
于 2009-03-16T14:14:53.153 回答