Skip to content

Bug: Incorrect type inference for varchar column as UUID in where query with JRuby JDBC adapter #1197

@ubinexy-tw

Description

@ubinexy-tw

Overview

I've encountered an error when using the JRuby JDBC adapter for PostgreSQL. It seems ActiveRecord incorrectly infers the data type of string values as uuid if its format resembles a UUID,. This happens even if the corresponding database column is explicitly defined as character varying (varchar).

Steps to Reproduce

The following standalone script reliably reproduces the issue.

# frozen_string_literal: true

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'activerecord', '~> 7.1.5.2'
  gem 'activerecord-jdbcpostgresql-adapter', '~> 71.0'
  gem 'jruby-openssl'
end

require 'active_record'
require 'logger'

# 1. Database Configuration
db_config = {
  adapter:  'jdbcpostgresql',
  host:     'localhost',
  database: ENV['db_database'],
  username: ENV['db_username'],
  password: ENV['db_password']
}

ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Base.logger = Logger.new(STDOUT)

# 2. Schema and Model
ActiveRecord::Schema.define do
  drop_table :items, if_exists: true
  create_table :items do |t|
    t.string :external_id, limit: 36 # Explicitly character varying
    t.string :name
  end
end

class Item < ActiveRecord::Base
end

# 3. Seed Data
Item.create!(external_id: 'a068351235251000f07fa89bcaf80000', name: 'Existing Item 1')
Item.create!(external_id: 'e2d08afc53614c37b32b31270bb8bee3', name: 'Existing Item 2')

# 4. Reproduce the Bug
ids_to_include = [
  {
    id: 'a068351235251000f07fa89bcaf80000'
  },
  {
    id: 'a068351235251000f07fa89bcaf80002'
  }
].map { |item| item[:id] }

puts "\n--- DEMONSTRATING THE BUG ---"
begin
  # This query fails due to incorrect type inference
  failing_query = Item.where(external_id: ids_to_include)
  failing_query.to_a
  puts "✅ SUCCESS (NO BUG): The query worked unexpectedly."
rescue ActiveRecord::StatementInvalid => e
  puts "❌ FAILURE (BUG REPRODUCED): The query failed as expected."
  puts "   Error Class: #{e.class}"
  puts "   Error Message: #{e.message}"
end

puts "\n--> Script finished."

Expected Behavior

The query Item.where(workday_style_id: ids_to_include) should execute successfully. It should generate a SQL query where the bind parameters for the IN clause are treated as strings (varchar), matching the column type.

Actual Behavior

However, the query fails with an ActiveRecord::StatementInvalid exception. The underlying PostgreSQL error is PG::UndefinedFunction: ERROR: operator does not exist: character varying <> uuid.

This indicates that the adapter sent the string parameters to the database typed as uuid instead of varchar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions