简体   繁体   中英

MySQL delete and remove from many-to-many table in one transaction

Is it possible to delete from association table (many-to-many) and insert same row to it in the same transaction?

This is my association table:

CREATE TABLE image_tag (
  imageid bigint(19) NOT NULL, 
  tagid   bigint(19) NOT NULL, 
  PRIMARY KEY (imageid, tagid));
ALTER TABLE image_tag ADD INDEX FKimage_tag587679 (tagid), ADD CONSTRAINT  FKimage_tag587679 FOREIGN KEY (tagid) REFERENCES tag (id);
ALTER TABLE image_tag ADD INDEX FKimage_tag426448 (imageid), ADD CONSTRAINT FKimage_tag426448 FOREIGN KEY (imageid) REFERENCES image (id);

When I am creating a new image and inserting into image, insert into tag and insert into image_tag in one transaction, everything works.

The problem occurs when I want to to update Image and I:

  • start transaction
  • update image with given $id
  • call: DELETE FROM image_tag WHERE imageid = $id
  • check whether image_tag is empty for imageid = $id (and it is)
  • retrieve IDs of new tags (some of them remains same)
  • try to insert into image_tag ...but I receive an exception

Exception:

Cannot add or update a child row: a foreign key constraint fails (`mydatabase`.`image_tag`, CONSTRAINT `FKimage_tag426448` FOREIGN KEY  (`imageid`) REFERENCES `image` (`id`))  

Data:

Original data: (194, 123), (194, 225), (194, 291)
New data: (194, 123), (194, 225), (194, 65)

Source code (in PHP):

if ($editation) {
  if ($values->image->isOk()) {
    $url = $img->load($values->id)->getUrl();
    $values->image->move($this->wwwDir . "/" . $url);
  } else {
    $url = null;
  }      
  $img->update($values->name, $values->descr, $url, null, $featured);
} else {
  $uname = $img->createUniqueName($values->name);
  $url = ImageManager::PATH . "/" . $uname . ".svg";
  $values->image->move($this->wwwDir . "/" . $url);
  $img->create($userId, $values->name, $url, $values->descr, null, $featured);      
}

// Delete old image-tag associations if any
$this->db->query('DELETE FROM image_tag WHERE imageid = %i', $img->getId());

// Save tags and assign them to image
$tagIds = $this->tagManager->saveTags($values->tags);
$assoc = (new Dao\ImageTag())->setDb($this->db);
//throw new \Exception(implode(",", $tagIds));
foreach ($tagIds as $tagId) {     
  // EXCEPTION IS THROWED HERE (but only after editation)
  $assoc->create($img->getId(), $tagId);      
}

Don't you try to update your image_tag with invalid id? ..null, 0, -1 or something else?

Look to your code. I suppose $img and $values->image are not the same, but you load $img only when $values->image is provided. However, you try to update image_tag every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM