Question

I have some problems using mnesia:select/2 but I think they are related to MatchHead. In my code I have the following record:

-record(pet_info, {name, id, type}).

I put some values using this record in a Mnesia table (i.e. pet_table). I checked they are actually in table using tv:start()).

I want to retrieve all the ids but my code returns always an empyt list.

This is the salient part of my code

F = fun() ->
            Pet = #pet_info{id = '$1', _ = '_'},
            mnesia:select(pet_table, [{Pet, [], ['$1']}])
    end,
Reply = case mnesia:transaction(F) of
            {atomic, ResultOfFun} ->
                ResultOfFun;
            {aborted, _Reason} ->
                {error, aborted}
        end,
Reply.

Can you tell me where is the error?

Was it helpful?

Solution

I have found a solution for my problem.

The table was created as:

mnesia:create_table(pet_table, [{disc_copies, [node()]},
                                {type, set},
                                {attributes, record_info(fields, pet_info)}])

and this didn't work.

On the other hand by changing the name of the table to the same name used for the record it works.

mnesia:create_table(pet_info, [{disc_copies, [node()]},
                                {type, set},
                                {attributes, record_info(fields, pet_info)}])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top