SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2009
    Location
    Bristol
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    INSERT and UPDATE the same row in the same TRANSACTION?

    So here's my problem:

    I have an article submission form with an optional image upload field.

    The submission uses three steps:

    1: Transaction starts
    2: $_POST array is processed, new row added - id generated by UUID()
    3: $_FILES array is processed, image file resized and saved with the name of the inserted row and the appropriate image extension. If this fails - then the transaction is rolled back, and the function returns false.
    4: The original row is updated with the name of the image. If this fails, the transaction is rolled back.

    I've designated a composite UNIQUE key ('blog_title','post_url')

    However, I'm getting a DUPLICATE exception on the new ID generated in step 1.
    Is this an inappropriate use of transactions?

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2009
    Location
    Bristol
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - just for the record, this is entirely possible. The problem was in my application architecture. I was catching Exceptions in my Mapper classes that were handling persistence - and then querying them to return boolean states and thus interrupt the process. This was in turn breaking the try/catch loop which was preventing the insert/update from working correctly. To summarise - Yes - you CAN insert and update the same row in a single transaction.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •