Search code examples
erlangmnesia

Cant setup Mnesia


setup_mnesia(Name) ->
    ?VALUE(application:start(mnesia)),
    ?VALUE(mnesia:create_schema([node()|[Name]])),
    ?VALUE(mnesia:create_table(muppet, [
                        {attributes, record_info(fields, muppet)},
                        {disc_copies, [foo@kos13]}])),
    ?VALUE(mnesia:wait_for_tables([muppet], infinity)),
    ok.

result is

"application : start ( mnesia )" = ok

"mnesia : create_schema ( [ node ( ) | [ Name ] ] )" = {error, {foo@kos13, {already_exists, foo@kos13}}}

"mnesia : create_table ( muppet , [ { attributes , record_info ( fields , muppet ) } , { disc_copies , [ foo@kos13 ] } ] )" = {aborted, {bad_type, muppet, disc_copies, foo@kos13}}

EDITED, ADDED if rewrite both processes to call application:start after mnesia:create_schema it spits out "Cannot install fallback". In current directory appear two files - FALLBACK.BUP and foo@kos13131851070846165246847780.


Solution

  • If your schema is a ram_copies type, mnesia cannot allow any other table to reside on disc, all tables you create in a ram_copies schema will also be in RAM.

    Another thing, specify a mnesia dir like this:

    erl -name MY_NODE_NAME -mnesia dir '"./Databases/MY_NODE_NAME_DATABASE"'
    

    Where, ./Databases/MY_NODE_NAME_DATABASE must be an existing folder. After this, then you can create your schema and tables as documented.

    Another thing you can do if your schema is in RAM is the function: mnesia:change_table_copy_type(Table_name, On_which_Node, To_new_type). After changing the schema type to disc_copies or disc_only_copies, then you can change the type of your tables as well to disc. If the schema is a disc_copies type as it normally will be, you can have tables of any nature you want, whether RAM, Disc or Disc_only_copies.

    Its possible to get rid of unwanted schemas using mnesia:delete_schema/1, but be very careful with this method.

    Any thing beyond this , please do refer to the mnesia users guide.