0

I am trying to use boss_db for accessing pgsql. The table should have the column name, id, it should be primary key

The Id type is only be uuid or serial. Is it right?

I want that id is varchar(20), the value of id is decided by the program, not auto decided by DBMS. Is it possible?

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"),

The follwing codes is from boss_sql_lib.erl file:

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}.
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
Chen Yu
  • 3,955
  • 1
  • 24
  • 51

1 Answers1

1

Assign an ID when you create the BossRecord by using your own ID rather than the atom 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

Record IDs are typically created by the framework. They must be globally unique. I recommend allowing the framework to assign its own IDs, rather than creating them manually as you will probably encounter bugs. However, there is nothing stopping you doing whatever you want.

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • Thank you very much. As what you pointed, the id problem has been solved aleady. For the second problem, boss_db conflict with pgsql when using pgsql's schema feature, because the "." has reserved word for boss_db. – Chen Yu Aug 02 '13 at 05:59