Thank you for your answer, but I'm not sure if I got your point Ryan. Are you 
saying that I should simply add "a" and "b" into table C?

If so, I think it's not the problem that I'm facing. Before I insert "a" and 
"b" into table C, I must ensure that there is "a" in table A and there is "b" 
in table B. If there is neither "a" nor "b" previously I should insert them 
into A and B, respectively.

That's why I titiled this email with "Thinking asynchronously" because it's 
hard to me (newbie in node) to asynchronize this code.

In my original email I posted a code that's not working. What am I doing wrong 
on it?

Emerson de Lira Espínola


Em 25/02/2012, às 17:00, Ryan Schmidt <[email protected]> escreveu:

> 
> On Feb 24, 2012, at 13:19, Emerson Espínola wrote:
> 
>> I have table A and B and a n to n relationship called C.
>> In order to insert something in table C I must check if the primary key of A 
>> and B exist in their respective tables before with a SELECT statement.
>> Is there any sample code that does that?
> 
> Surely coding that way is a race condition. (Between the time you run the 
> SELECT statement, and the time you run the INSERT statement, the database 
> could have been changed by other processes/users.) Surely instead you would 
> set up the join table structure so that there are appropriate unique 
> constraints. And then when you want to add a relationship between entities 
> A.x and B.y you simply INSERT the appropriate entry into table C. If it 
> succeeds, you're done; the relationship was added. If it fails, the unique 
> constraint was violated because a relationship between them already existed.
> 
> 
> -- 
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to