mnesia primary key

Posted by maryjanne on Stack Overflow See other posts from Stack Overflow or by maryjanne
Published on 2010-06-17T09:28:32Z Indexed on 2010/06/17 9:33 UTC
Read the original article Hit count: 581

Filed under:
|

Hi

I have two tables one notes and one tag and I want to make the id from notes primary key to use it in the tag table, but I don't know where I do wrong. My notes id is generate from another table counter, with the function dirty_update_counter.

My function for the id_notes from tag looks like this:

Fun = fun() -> 
                mnesia:write(#tag{ id_note =0}) 
        end, 
        mnesia:transaction(Fun). 

generate_Oid(TableName) when is_atom(TableName) -> 
        F = fun() -> 
                [Oid] = mnesia:read(tag, TableName, write), 
                NewId = Oid#tag.id_note+1, 
                New = Oid#tag{id_note = NewId}, 
                mnesia:write(New), 
                NewId 
        end, 
        mnesia:transaction(F). 

insert_n(N) when is_record(N, note) -> 
        F = fun() -> 
        {atomic, Id} = generate_Oid(note), 
        New = N#note{id = Id}, 
        mnesia:write(New), 
        New 
        end, 
        mnesia:transaction(F). 

find_n(Id) when is_integer(Id) -> 
        {atomic, [N]} = mnesia:transaction(fun() -> 
                mnesia:read({note, Id}) 
                        end), 
N.

But this function don't increment my field id_note from the table tag, despite the fact that in my note table, my id field is incremented from counter table.

Thanks in advance for any help.

© Stack Overflow or respective owner

Related posts about erlang

Related posts about mnesia