What is :mnesia.last() used for?

I’m currently making a blockchain project, and I’m trying to use mnesia to store and query data.
I want do get the last written block, but why :mnesia.last() don’t work?

Works exactly like mnesia:first/1, but returns the last object in Erlang term order for the ordered_set table type. For all other table types, mnesia:first/1 and mnesia:last/1 are synonyms.

Are you sure your table type is correct?

1 Like

thank you, this time I used this

:mnesia.create_table(:block, [attributes: [:timestamp, :prev_hash, :hash, :data], type: :ordered_set])

the first() and last() aren’t the same now, but it still couldn’t get the right one(the first or the last created)
by the way, how to store the db in disk instead of memory

6 posts were split to a new topic: Offtopic posts from :mnesia.last question

What kind of value are you using for :timestamp? The reason that I ask, is that the order is determined according to basically the “raw” sort order of the data. So if you’re using %DateTime{} structs, it isn’t going to sort correctly, since it would sort those by their “map” structure and not their value as a time. You should use integer timestamps as the timestamp if you want the mnesia sorting to work.

1 Like

oh! Thanks a lot!
I used NaiveDateTime and now I’m using an index to make it able to sort
It works!