Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  [newbie] Cannot handle Mnesia :(

sv75
Posted: Wed Oct 24, 2007 10:37 am Reply with quote
User Joined: 19 Oct 2007 Posts: 10
I spent two hours reading manuals and still cannot get it working as I want. Hope somebody point me my error.

usecase: I try to run it on single node from cli and just save data on disk. That's all. Still no luck. I blame myself. Source cod, output is below:

Code:

-module(m2).
-export([start/0]).
-record(test, {id, data}).

start() ->
    io:format("Node: ~w~n", [node()]),   
    io:format("schema: ~w~n", [mnesia:create_schema([node()])]),
    ok = mnesia:start(),
    io:format("create table: ~w~n", [mnesia:create_table(test,
        [{disc_copies, [node()]},
         {type, bag},
         {attributes, record_info(fields, test)}])]),
    io:format("write: ~w~n", [mnesia:transaction(
        fun() -> mnesia:write(#test{id = 1, data = "aaaa"}) end)]),
    stopped = mnesia:stop().


First run: everything looks ok, directory is created too:
Code:

$ erl -noshell -s m2 start -s init stop -sname node
Node: 'node@seva-work'
schema: ok
create table: {atomic,ok}
write: {atomic,ok}


Second run. It does not work:
Code:

Node: 'node@seva-work'
schema: {error,{'node@seva-work',{already_exists,'node@seva-work'}}} # fine
create table: {aborted,{already_exists,test}} # ok, fine
write: {aborted,{no_exists,test}} # but why???
View user's profile Send private message Send e-mail
francesco
Posted: Wed Oct 24, 2007 1:11 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
The second run fails because you have already created the schema and the table. They are persistent. Remove the directory created by Mnesia, and all will work as expected. Else, skip the create_schema and the create_table.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
sv75
Posted: Wed Oct 24, 2007 1:17 pm Reply with quote
User Joined: 19 Oct 2007 Posts: 10
francesco wrote:
The second run fails because you have already created the schema and the table.

I wonder why 'write' fails, not 'create_*' Smile Sorry if it was not clean.
francesco wrote:

Else, skip the create_schema and the create_table.


skiping it does not change anything imho.

Code:

-module(m2).
-export([start/0]).
-record(test, {id, data}).
start() ->
    ok = mnesia:start(),
    io:format("write: ~w~n", [mnesia:transaction(
        fun() -> mnesia:write(#test{id = 1, data = "aaaa"}) end)]).


Output:
Code:

$ erl -noshell -s m2 start -s init stop -sname node
write: {aborted,{no_exists,test}}
View user's profile Send private message Send e-mail
francesco
Posted: Wed Oct 24, 2007 1:41 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Mnesia will load its existing in the background, immediately returning ok once you've called mnesia:start/0. What is probably happening is that test has not been loaded yet. Put mnesia:wait_for_tables([test]) before the write, and it should work.

Francesco

PS. Hope it is ok if I move this thread to the OTP section, as it is where it belongs
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
sv75
Posted: Wed Oct 24, 2007 1:47 pm Reply with quote
User Joined: 19 Oct 2007 Posts: 10
"RTFM" would be an appropirate answer...
mnesia:wait_for_tables([test], infinity)

Ok, I think I'm gonna teach some students now Smile
View user's profile Send private message Send e-mail

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum