tags:

views:

72

answers:

1
+1  Q: 

mnesia primary key

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.

A: 

The mnesia:dirty_update_counter/3 method might be what you are looking for.

dsmith
Thanks, first of all, very much for your answer.And my function looks like this:new_id(Id) -> mnesia:dirty_update_counter(note, Id, 1).