How do you create an mnesia schema in a specified directory from Application.start callback in Elixir?

3067 views elixir

So I have a Phoenix 1.3 application. I read that you have to create an mnesia schema before your application starts up so you can then create an application table.

In config.exs, I have:

config :mnesia, 'path/to/mnesia/#{node()}'

In application.ex:

defmodule MyApp do
  use Application

  def start(_, _args) do
        :mnesia.create_schema([node() | Node.list()])

Now, Mnesia.nonode@nohost gets created in the topmost level directory of my application if I comment out the line in config.exs. But if I do try and specify a directory in config.exs, I see a message like {:aborted, {:bad_type, :region, :disc_copies, :nonode@nohost}}

answered question

1 Answer


The issue is config is a compile-time beast, one cannot expect node() to be retrieved in the runtime. It’s compiled and hard-coded to what node() returns in compile-time.

That said if you compile your application on node foo, the config in runtime is hardcoded to 'path/to/mnesia/foo' on all nodes.

To fix this, you should store the prefix of the path in the config and configure mnesia in runtime on each node by appending '#{node()}' to the prefix.

posted this

Have an answer?


Please login first before posting an answer.