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
