[erlang-questions] Mnesia with Records Confusion

Robert Virding robert.virding@REDACTED
Tue Apr 9 02:47:12 CEST 2013


Mnesia uses the first record field, 'pid' in your case, as the *primary* index of a table. This is the key which is used when you do a straight read(Tab, Key). Adding an explicit {index,Keys} element to the table definition creates *extra* index tables for the table using that the fields in the Keys list. In your case 'pid''. Again.

I am guessing that having a field as both the primary key and an extra key causes it to fail. So when you insert a new first field 'id' this then becomes the primary key and you extra key 'pid' is no longer the primary key so it works.

Actually in this case explicitly defining the the first record field as a key is totally unnecessary as it it is already the primary key. Even if it worked. :-) And adding an extra first field 'id' just so you can can use the 'pid' field as an index and read it with index_read/3 is duplicating the work and is unnecessary.

If it is as you described it.

Robert

----- Original Message -----
> From: "Lee Sylvester" <lee.sylvester@REDACTED>
> To: "erlang-questions@REDACTED Questions" <erlang-questions@REDACTED>
> Sent: Tuesday, 9 April, 2013 12:06:42 AM
> Subject: [erlang-questions] Mnesia with Records Confusion
> 
> Hey guys,
> 
> So, I have a record that looks a little like this
> 
> -record(user, {pid, username}).
> 
> That's a simplified version.  Anyhoo, I was trying to add it to and
> remove it from a table in Mnesia, but kept getting a badarg error.
>  I'd set the table up like so:
> 
> 	mnesia:create_table(user,
> 		[
> 			{type,set},
> 			{record_name, user},
> 			{index, [pid]},
> 			{attributes, record_info(fields, user)}
> 		]),
> 
> This error had me baffled for a while as I was able to add records to
> the table, but couldn't read them with index_read.  I eventually
> found that the issue was to do with the index of the pid property in
> the record and, by adding an id property as the first field, all my
> problems disappeared.
> 
> Can someone please explain why this is so?  I mean, I'm glad it works
> now, but I'd love to know why I had to do this.
> 
> Thanks loads,
> Lee
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 



More information about the erlang-questions mailing list