我发现ActiveRecord delete_all 忽略了联接表条件。这是Rails社区的已知问题吗?
查询:
DesignPartCategory.joins(:category).where(category: {site_id: INDIA}, designpart_uuid: ['123C']).delete_all
预期结果:
从designpart_category
的{{1}}。designpart_category
IN('123C')和designpart_uuid
。designpart_category
IN(印度)删除
实际结果:
category_id
当前 delete_all 操作只是忽略/丢弃联接表条件并执行其余查询。
我假设情况并非如此,应该抛出异常而不是忽略联接。请让我们知道您对此的想法/评论。
答案 0 :(得分:1)
似乎您的语法中有一些错别字,请检查一下-
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
答案 1 :(得分:0)
您是否在查询中检查了以下内容?
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
中触发的查询
答案 2 :(得分:0)
delete_all
删除匹配条件的记录,而无需先实例化记录,因此不调用destroy方法或调用回调。因此,它不会考虑任何join
,includes
等问题。
Check the documentation