is delete\u all会忽略基于联接表的条件吗?

wribegjk  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(419)

我观察到activerecord delete\u all忽略了joins表的条件。这是rails社区的已知问题吗?
查询:

DesignPartCategory.joins(:category).where(category: {site_id: INDIA}, designpart_uuid: ['123C']).delete_all

预期结果:
从删除 designpart_category 哪里 designpart_category . designpart_uuid 在('123c')和 designpart_category . category_id 印度;
实际结果:

DELETE FROM `designpart_category` WHERE `designpart_category`.`designpart_uuid` IN (SELECT designpart_uuid FROM (SELECT `designpart_category`.`designpart_uuid` FROM `designpart_category` INNER JOIN `category` ON `category`.`category_id` = `designpart_category`.`category_id` WHERE (category.site_id =INDIA AND designpart_category.designpart_uuid in ('123C'))) __active_record_temp);

当前delete\u all操作只是忽略/放弃联接表条件并执行查询的其余部分。
我假设不是这样,它应该抛出异常而不是忽略连接。请让我们知道您的想法/意见。

toe95027

toe950271#

你的语法好像有点错,看看这个-

DesignPartCategory.joins(:category).where("categories.site_id = ? AND categories.designpart_uuid IN (?)", 'INDIA', ['123C']).delete_all

或者你可以试试这个:

DesignPartCategory.joins(:category).where(categories: {site_id: 'INDIA', designpart_uuid: (['123C'])}).delete_all

注意:-删除vs销毁

gv8xihay

gv8xihay2#

delete_all 删除符合条件的记录,而不首先示例化记录,因此既不调用destroy方法,也不调用回调。所以它不会考虑像 join , includes 等。。。检查文档

k4ymrczo

k4ymrczo3#

您在查询中检查了以下内容吗?

DELETE FROM `designpart_category` 
  WHERE `designpart_category`.`designpart_uuid` IN (
    SELECT designpart_uuid FROM (
      SELECT `designpart_category`.`designpart_uuid` 
      FROM `designpart_category` INNER JOIN `category` 
      ON `category`.`category_id` = `designpart_category`.`category_id` 
      WHERE (category.site_id =INDIA AND designpart_category.designpart_uuid in ('123C')
    )
  ) __active_record_temp
);

它会根据您的连接查询进行过滤,检查已启动的查询 IN

相关问题