语言陷阱:mnesia中关于index的选择

erlang使用mnesia进行数据库操作的时候出现的bad_type错误。
对于下面的数据结构:

       -record(info, {name, id}).
       

使用下面的语句来创建表:

       case mnesia:create_table(info, [
                                    {disc_copies, Nodes},
                                     {index, [name]},
                                    {type, set},
                                    {attributes, record_info(fields, info)}
                                     ]) of
        {atomic, ok} ->
            ok;
        Any ->
            error_logger:error_report([
                                       {message, "Cannot install table"},
                                       {table, info},
                                       {error, Any},
                                       {nodes, Nodes}])
    end.
       

编译的时候不会有任何问题,但运行的时候,会出现{aborted,{bad_type,info,{index,[2]}}}的错误,原因在于使用record的第一个元素来做index,如果不是第一个的话则没有任何问题。

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据