繁体   English   中英

ActiveRecord#sanitize_sql_array 方法将 integer 更改为字符串

[英]ActiveRecord#sanitize_sql_array method changes integer to string

我最近将我的应用程序的 rails 版本从 6.1 升级到了 7。升级后,我发现sanitize_sql_array现在正在将 integer 值更改为字符串。

以下是我传递给该方法的属性:

updates = ["`bed_count` = `bed_count` + ?", "`operating_room_count` = `operating_room_count` + ?", "`updated_at` = ?"]
values = [3.14159, 1, "2022-09-17 18:15:05"]

现在,当运行该方法时,我得到以下 output:

>> ActiveRecord::Base.send(:sanitize_sql_array, [updates.join(','), *values])
=> "`bed_count` = `bed_count` + '3.14159',`operating_room_count` = `operating_room_count` + '1',`updated_at` = '2022-09-17 18:33:44'"

请注意,即使我提供了 integer 值,现在 3.14159 和 1 也已更改为字符串。

当我在 Rails 6.1 中运行相同的代码时,我得到了以下 output:

"`bed_count` = `bed_count` + 3.14159,`operating_room_count` = `operating_room_count` + 1,`updated_at` = '2022-09-17 18:33:44'"

有谁知道我该如何解决这个问题?

似乎与https://github.com/rails/rails/pull/42440有关?

出于安全原因,MySQL 适配器现在将数字和布尔值绑定到字符串。

我猜他们正在解决您的问题: https://github.com/rails/rails/pull/45379

认为现在最好的解决方法是将字符串转换为 SQL 中的整数/小数,如下所示:

...bed_count` + CAST(? as UNSIGNED)
# or 
CAST(? as DECIMAL)

暂无
暂无

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

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