简体   繁体   English

MySQL在一次交易中从多对多表中删除和删除

[英]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. 当我创建一个新图像并插入到图像中时,在一个事务中插入到tag并插入到image_tag,一切正常。

The problem occurs when I want to to update Image and I: 当我想更新Image时出现问题,我:

  • start transaction 开始交易
  • update image with given $id 使用给定的$ id更新图像
  • call: DELETE FROM image_tag WHERE imageid = $id 呼叫:从image_tag删除WHERE imageid = $ id
  • check whether image_tag is empty for imageid = $id (and it is) 检查image_tag是否为imageid = $ id为空(是)
  • retrieve IDs of new tags (some of them remains same) 检索新标签的ID(其中一些保持不变)
  • try to insert into image_tag ...but I receive an exception 尝试插入image_tag ...但是我收到异常

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): 源代码(在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? 您不尝试使用无效ID更新您的image_tag吗? ..null, 0, -1 or something else? ..null,0,-1还是其他?

Look to your code. 查看您的代码。 I suppose $img and $values->image are not the same, but you load $img only when $values->image is provided. 我想$ img和$ values-> image是不一样的,但是只有在提供$ values-> image时才加载$ img。 However, you try to update image_tag every time. 但是,您尝试每次更新image_tag。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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