Rails 7.2.2.1迁移遇PG::InvalidTextRepresentation错误:整数列绑定浮点数
Rails 7.2.2.1 迁移后绑定变量查询整数列报错问题
我们正计划从当前运行的Rails 7.1.5.1迁移至Rails 7.2.2.1,在CI运行过程中发现使用绑定变量查询整数列时出现错误:当传入浮点数参数匹配整数类型字段时,PostgreSQL抛出类型转换错误。
Rails 7.2.2.1 报错示例
# Loading development environment (Rails 7.2.2.1) FulfillmentZone.where("fulfillment_zones.maximum_zone_quantity >= :quantity", quantity: 15.0).to_a FulfillmentZone Load (0.9ms) SELECT "fulfillment_zones".* FROM "fulfillment_zones" WHERE (fulfillment_zones.maximum_zone_quantity >= $1) [[nil, 15.0]] ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type integer: "15.0" (ActiveRecord::StatementInvalid) CONTEXT: unnamed portal parameter $1 = '...' from /Users/jlamarque/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:912:in `exec_prepared' Caused by PG::InvalidTextRepresentation: ERROR: invalid input syntax for type integer: "15.0" CONTEXT: unnamed portal parameter $1 = '...' from /Users/jlamarque/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:912:in `exec_prepared'
Rails 7.1.5.1 正常表现
相同查询在Rails 7.1.5.1中可正常执行,不会报错:
# Loading development environment (Rails 7.1.5.1) FulfillmentZone.where("fulfillment_zones.maximum_zone_quantity >= :quantity", quantity: 15.0).to_a FulfillmentZone Load (1.4ms) SELECT "fulfillment_zones".* FROM "fulfillment_zones" WHERE (fulfillment_zones.maximum_zone_quantity >= 15.0) #=> [#<FulfillmentZone:0x00000001293d0ea8 id: 1, fulfillment_service_id: 1, name: "France", deleted_at: nil, created_at: Wed, 18 Sep 2024 12:06:57.819485000 CEST +02:00, updated_at: Wed, 18 Sep 2024 12:06:57.819485000 CEST +02:00, maximum_zone_quantity: 100>, #<FulfillmentZone:0x0000000129218c00 id: 3, fulfillment_service_id: 3, name: "FRANCE", deleted_at: nil, created_at: Tue, 31 Dec 2024 09:32:23.843419000 CET +01:00, updated_at: Tue, 31 Dec 2024 09:32:23.843419000 CET +01:00, maximum_zone_quantity: 100>]
已排查信息
排查了ActiveRecord的PostgreSQL适配器库(ActiveRecord::ConnectionAdapters::PostgreSQL::Quoting),发现类型转换结果由抽象引用模块的type_cast方法返回,该方法对于数值类型未做转换,直接返回原值,进而触发PostgreSQL的无效语句错误。
请问各位有什么解决思路?
内容的提问来源于stack exchange,提问作者user30719238
相关产品推荐
相关产品推荐

