Data Explorer `int_list` 将第一个元素替换为0

不确定这是 bug 还是我操作有误,但在数据探索器查询中,int_list 数组的第一个元素似乎总是被替换为 0。

例如:

-- int_list :categories = "3, 5, 6"

会被改为 [0, 5, 6]

查看:

如果我在控制台中执行,会得到预期的输出:

string = "3,5,6"
value = string.split(',').map { |s| s.downcase == '#null' ? nil : s.to_i }
=> [3, 5, 6]

您可以使用以下查询复现此问题:

-- [params]
-- int_list :categories = "3, 5, 6"

SELECT *
FROM topics 
WHERE topics.category_id = ANY (ARRAY [ :categories ]  )

要触发错误(以便查看错误信息),只需删除右括号,例如:

-- [params]
-- int_list :categories = "3, 5, 6"

SELECT *
FROM topics 
WHERE topics.category_id = ANY (ARRAY [ :categories   )
PG::SyntaxError: ERROR:  syntax error at or near ")"
LINE 12: WHERE topics.category_id = ANY (ARRAY [ 0,5,6   )
                                                         ^

(查看该数组)

1 个赞

It looks like what is happening when you set a default value for an int_list parameter in this way:

-- int_list :categories = "3, 5, 6"

is that the following gets run:

'"1'.downcase.to_i

That will return 0.

You can get around the issue by leaving the quotes off the default parameter:

-- int_list :categories = 3, 5, 6

That value still gets interpreted as a string before being split into an array. Maybe the plugin should strip outer quotation marks if they get added to a string input.

6 个赞

Thanks Simon! :smiley:

I was sure I had tried that… and looking again now I see that changing default parameters in the body and clicking on save and run has no effect - you either have to refresh the page after saving or change the values in the box for the field as well (which I totally forgot about).

2 个赞

FWIW I find params really fiddly to reason about, I wonder if we should shift this to full UX vs magic comments @riking

5 个赞

Yup, totally makes sense to do that, the magic comments were optimizing for minimum # of plugin-sourced DB migrations, which are much less of a problem these days.

5 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.