Codeigniter/mySQL question. Checking if several inserts are possible before actual insertion.

Posted by Ethan on Stack Overflow See other posts from Stack Overflow or by Ethan
Published on 2010-04-19T21:49:37Z Indexed on 2010/04/19 21:53 UTC
Read the original article Hit count: 245

Filed under:
|
|

Hey SO,

So I'm trying to perform a couple inserts at once that are kind of co-dependent on each other. Let's say I'm doing a dog rating website. Anyone can add a dog to my database, but in doing so they also need to add a preliminary rating of the dog. Other people can then rate the dog afterwards. The dogs to ratings is a many to one relationship: a dog has many ratings.

This means for my preliminary add, since I have the person both rate and add the dog, I need to take the rating, and set a foreign key to the dog's primary key. As far as I know, this means I have to actually add the dog, then check what that new addition's primary key is, and then put that in my rating before insertion.

Now suppose something were to go wrong with the rating's insert, be it a string that's too long, or something that I've overlooked somehow. If the rating's failed, the dog has already been inserted, but the rating has not. In this case, I'd like the dog to not have been added in the first place.

Does this mean I have to write code that says "if the rating fails, do a remove for the dog," or is there a way to predict what the key will be for the dog should everything go as planned. Is there a way to say "hold that spot," and then if everything works, add it?

Any help would be greatly appreciated. Thanks!!

© Stack Overflow or respective owner

Related posts about codeigniter

Related posts about mysql