views:

84

answers:

2

I am experimenting with ErlyDB in a non-erlyweb environment and I am not having much luck.

I have a 'Thing' table for testing, and a corresponding Thing module:

-module(thing).
-export([table/0, fields/0]).

table() ->
     thing.
fields() ->
    [name, value].

The module itself works - I can query the database fine using ([Thing] = thing:find({name, '=', "test"})).

When I try and save a new record, however things aren't so good.

I consistently see the following error:

mysql_conn:426: fetch <<"BEGIN">> (id <0.97.0>)
mysql_conn:426: fetch <<"INSERT INTO thing(value,name) VALUES ('vtha','blah')">> (id <0.97.0>)
mysql_conn:426: fetch <<"ROLLBACK">> (id <0.97.0>)
** exception exit: {{'EXIT',{badarg,[{erlang,hd,[[]]},
                                     {erlydb_base,'-do_save/1-fun-0-',4},
                                     {mysql_conn,do_transaction,2},
                                     {mysql_conn,loop,1}]}},
                    {rollback_result,{updated,{mysql_result,[],[],0,[]}}}}
     in function  erlydb_base:do_save/1
     in call from erlydb_base:hook/4
     in call from test_thing:test/0
        called as test_thing:test()

The table exists, the credentials work, and the SQL itself is fine, as I can execute the command directly on the database.

The code I am using to save is:

erlydb:start(mysql, Database),
Thing = thing:new(<<"hello">>, <<"world">>),
thing:save(Thing),

Is there something I am missing? Is there some way of viewing some more helpful error messages from the database?

+3  A: 

Looking at the source of erlydb_base, the exception happens when erlydb calls your thing module's db_pk_fields() function. That function should return a list, but apparently it doesn't.

Zed
Yes - I think it is actually related to this issue: http://groups.google.com/group/erlyweb/browse_thread/thread/a719be16b83bcc0f/2d1f7f4c72dbdef4?lnk=raot#2d1f7f4c72dbdef4
Toby Hede
+1  A: 

I can confirm that altering the code in erlydb.erl fixes this problem (from this reference on the mailing list).

Change Line 561 of erlydb.erl from

lists:map(fun({_Name, _Atts} = F) -> F;
      (Name) -> {Name, []}
      end, lists:usort(DefinedFields)),

To:

lists:map(fun({_Name, _Atts} = F) -> F;
      (Name) -> {Name, []}
      end, DefinedFields),
Toby Hede