Search code examples
erlangelixirmnesia

Why is my mnesia index in this position?


iex(6)> :mnesia.table_info(:users, :attributes)
[:id, :email, :foo, :inserted_at, :updated_at]

iex(7)> :mnesia.table_info(:users, :index)     
[3]

iex(8)> :mnesia.add_table_index(:users, :email)
{:aborted, {:already_exists, :users, 3}}

I get that Tab is the first index, but then why isn't the index on 2 instead of 3? Are the indexes 1 based instead of zero, or is something else at play here?


Solution

  • Mnesia tables contain tuples that have a record-like format. In your example the tuple that is stored in the :users table looks something like:

    {:users, 1, "f@b.com", "foo", {2016, 12, 24}, {2016, 12, 31}}
    

    Which maps to:

    Index |   1    |   2   |   3    |  4   |      5       |     6       |
    Name  | :users |  :id  | :email | :foo | :inserted_at | :updated_at |
    

    Since tuples are 1-based an index created for the :email value will be created on position 3 of the tuple.