【发布时间】:2014-02-18 12:52:08
【问题描述】:
-record(ng, {ng}).
mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).
我收到:{aborted,{bad_type,ng,{attributes,[ng]}}} 错误。
怎么了?如何创建一个包含一列(已命名)的 mnesia 表?
【问题讨论】:
-record(ng, {ng}).
mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).
我收到:{aborted,{bad_type,ng,{attributes,[ng]}}} 错误。
怎么了?如何创建一个包含一列(已命名)的 mnesia 表?
【问题讨论】:
记录必须至少有 2 个字段。这会起作用:
-record(ng, {ng, extrafield}).
mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).
来自http://www.erlang.org/doc/man/mnesia.html#create_table-2
“表必须至少有一个额外的属性除了键。”
编辑:无法找到关于单列是否可能的答案,但this 2007 thread 表示不可能。
我个人用键/值列来做,像这样:
-record(proximaglobal, {key, value}).
mnesia:create_table(proximaglobal, [{attributes, record_info(fields, proximaglobal)}, {disc_only_copies, [node()]}]).
mnesia:sync_transaction(fun() -> mnesia:write(#proximaglobal{key=time, value=WorldTime}) end).
mnesia:sync_transaction(fun() -> mnesia:read(proximaglobal, time) end).
【讨论】:
{attributes, AtomList} 应该填充表的记录的属性名称列表。默认值为 [key, val]。除了键之外,该表还必须至少具有一个额外的属性。
【讨论】: