Bruce Kissinger - 2012-06-19

Amnesia allows you to specify the size of a VARCHAR column or just use a default length.  If you specify the column size, there is a problem with some queries. 

For example, if you define a table named LCC_CODES like this:

table (lcc_codes) ->
  [ {code, {varchar, 20}, },
    {meaning, {varchar, 1024}, }
    ];

running this code

Running this code:

    amnesia:add_new (Pid, ).

Generates error:

{error,"#42S22Unknown column 'A' in 'field list'",}

The fix is to modify the amnesia.erl file and change the cast_to_sql function around line 1674 as follows:

%% ===================================================================
%%  Function: cast_to_sql/2
%% ===================================================================
cast_to_sql (null, _) -> "null";

cast_to_sql (Value, Type) when Type == varchar;
                               Type == char;
                               Type == text ->
  lists:flatten ();

% Added to resolve VARCHAR defect
cast_to_sql (Value, {varchar, _}) ->
  lists:flatten ();
cast_to_sql (Value, {char, _}) ->
  lists:flatten ();
cast_to_sql (Value, {text, _}) ->
  lists:flatten ();
% end of Addition

cast_to_sql (Value, int) -> integer_to_list (Value);