Search code examples
erlangmnesia

Records in mnesia


I have the following small table in Erlang mnesia database. When I use the dirty_select function as follows:

mnesia:dirty_select(user, [{#user{id = '$1', name = martin}, [], ['$1']}]).

I get the error:

* 1: record user undefined

The user table has one row with user id as primary key and 'martin' as the name of the user. When I use the following command, it works well:

mnesia:dirty_read(user, 1).

And the output is:

[{user,1,martin}]

What could be the reason for the above error?


Solution

  • if you are getting this error when using the erlang shell, you will need to define the record. records are a compile time construct and the shell does not have access to their definitions.

    1> rd(user, {id, name}).
    user
    2> #user{id=1, name="foo"}.
    #user{id = 1,name = "foo"}
    3> mnesia:dirty_select(user, [{#user{id = '$1', name = martin}, [], ['$1']}]).
    ...
    

    once you have defined the record, your dirty_read operation will print the result using record syntax.