Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 173855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:25:01+00:00 2026-05-11T13:25:01+00:00

Suppose I’ve got a simple record definition: -record(data, {primary_key = ‘_’, more_stuff = ‘_’}).

  • 0

Suppose I’ve got a simple record definition:

-record(data, {primary_key = '_', more_stuff = '_'}). 

I want a simple function that adds one of these records to a mnesia database. But I want it to fail if there’s already an entry with the same primary key.

(In the following examples, assume I’ve already defined

db_get_data(Key)->     Q = qlc:q([Datum                || Datum = #data{primary_key = RecordKey}                       <- mnesia:table(data),                   RecordKey =:= Key]),     qlc:e(Q). 

)

The following works, but strikes me as sort of ugly …

add_data(D) when is_record(D, data)->     {atomic, Result} = mnesia:transaction(fun()->                                                   case db_get_data(D#data.primary_key) of                                                       [] -> db_add_data(D);                                                       _ -> {error, bzzt_duplicate_primary_key}                                                   end                                           end),      case Result of         {error, _} = Error -> throw(Error);         _ -> result     end. 

This works too, but is also ugly:

add_data(D) when is_record(D, data)->     {atomic, Result} = mnesia:transaction(fun()->                                                   case db_get_data(D#data.primary_key) of                                                       [] -> db_add_data(D);                                                       _ -> throw({error, bzzt_duplicate_primary_key})                                                   end                                           end). 

It differs from the above in that the above throws

{error, bzzt_duplicate_primary_key}, 

whereas this one throws

{error, {badmatch, {aborted, {throw,{error, bzzt_duplicate_primary_key}}}}} 

So: is there some convention for indicating this sort of error? Or is there a built-in way that I can get mnesia to throw this error for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:25:02+00:00Added an answer on May 11, 2026 at 1:25 pm

    I think both of them are fine, if you only make your code more pretty, like:

    add_data(D) when is_record(D, data)->      Fun = fun() ->                   case db_get_data(D#data.primary_key) of                       [] -> db_add_data(D);                       _  -> throw({error, bzzt_duplicate_primary_key})                   end           end,      {atomic, Result} = mnesia:activity(transaction, Fun). 

    or

    add_data(D) when is_record(D, data)->      Fun = fun() ->                   case db_get_data(D#data.primary_key) of                       [] -> db_add_data(D);                       _  -> {error, bzzt_duplicate_primary_key}                   end           end,      {atomic, Result} = mnesia:activity(transaction, Fun),      case Result of         {error, Error} -> throw(Error);         _              -> result     end. 

    Do you throw errors or return errors? I would return an error myself. We split out code out into mnesia work units – a module with a set of functions that perform basic mnesia activities not in transactions, and an api module which ‘composes’ the work units into mnesia transactions with functions that look very similar to the one above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No. There is just one pointer which initially is at… May 16, 2026 at 5:36 pm
  • Editorial Team
    Editorial Team added an answer You can try it with com4j. http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.open2000.aspx Since there is… May 16, 2026 at 5:36 pm
  • Editorial Team
    Editorial Team added an answer Couple solutions stand out: Store the names & classes in… May 16, 2026 at 5:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Suppose I'm entertaining a business model that offers free access to my propritary data
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a list of sets and I want to get the union
Suppose I have a table a with one column b and three rows(1,2,3), I
Suppose I'm using a placeholder jQuery plugin that reads the placeholder attribute from input
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have the following CSS rule in my page: body { font-family: Calibri,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.