使用boss_db时的限制?

时间:2013-06-17 15:30:01

标签: erlang chicagoboss

我正在尝试使用boss_db来访问pgsql。该表应具有列名,id,它应该是主键

Id类型只是uuid或serial。是不是?

我希望id为varchar(20),id的值由程序决定,而不是由DBMS自动决定。有可能吗?

create table operators(
       id  serial primary key, /*I want id is varchar(20), is it possible*/
       tag_id   varchar(20),
       name     text,
       barcode  varchar(20),
       tel      varchar(12),
       mobile   varchar(20),
       email    text,
       ldap_user_id             varchar(20),
       operator_barcode             varchar(20)
);

A = operator:new(id,"0102030405060708",
                       "operator_01","B001","12345678",
                       "13812345678",
                       "p001@gmail.com",
                       "ldap001",
                       "PB001"),

以下代码来自boss_sql_lib.erl文件:

infer_type_from_id(Id) when is_list(Id) ->
    [Type, TableId] = re:split(Id, "-", [{return, list}, {parts, 2}]),
    TypeAtom = list_to_atom(Type),
    IdColumn = proplists:get_value(id, boss_record_lib:database_columns(TypeAtom)),
    IdValue = case keytype(Type) of
                uuid -> TableId;
                serial -> list_to_integer(TableId)
            end,
    {TypeAtom, boss_record_lib:database_table(TypeAtom), IdColumn, IdValue}.

1 个答案:

答案 0 :(得分:1)

使用您自己的ID而不是原子id创建BossRecord时分配ID:

> AutoId = operator:new(id,
                   "0102030405060708",
                   "operator_01","B001","12345678",
                   "13812345678",
                   "p001@gmail.com",
                   "ldap001",
                   "PB001"),
> ManualID = operator:new("operator-01",
                   "0102030405060708",
                   "operator_01","B001","12345678",
                   "13812345678",
                   "p001@gmail.com",
                   "ldap001",
                   "PB001"),
> AutoID:save(),   % will just work
> ManualId:save(). % good luck

记录ID通常由框架创建。它们必须是全球唯一的。我建议允许框架分配自己的ID,而不是手动创建它们,因为您可能会遇到错误。然而,没有什么能阻止你做任何你想做的事。