Skip to content

Setting an enum attribute to nil fails with “Cannot determine type for nil values” #452

Open

Description

@amy-found

Environment details

  • Programming language: Ruby
  • OS: Linux but also reproduces on macOS
  • Language runtime version: Ruby 3.4.12, Rails/ActiveRecord 8.1.3.1
  • Package version: activerecord-spanner-adapter 2.5.0 (the same code is on main), google-cloud-spanner 2.36.0

Steps to reproduce

  1. Define a model with an enum backed by a nullable STRING(MAX) column:
class Card < ActiveRecord::Base
  enum :window_type, {
    calendar_day: "calendar_day",
    calendar_month: "calendar_month",
  }
end
  1. Create a record with the enum set, then set it to nil:
card = Card.create!(limit_window: "calendar_month")
card.update!(limit_window: nil)
  1. The update raises Cannot determine type for nil values, and the record isn’t saved. Setting the attribute to a non-nil value works.
    Expected: the column is set to NULL.

Thanks!

Activity

  1. added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Nov 6, 2026
  2. amy-found commented on Nov 6, 2026

    @amy-found
    Author

    In case this is helpful, here's some more info from when I dug into the issue:

    Cause

    SpannerActiveRecordConverter.convert_active_model_type_to_spanner doesn’t recognize ActiveRecord::Enum::EnumType. That type isn’t in TYPE_MAP and isn’t handled in fallback_convert_active_model_type_to_spanner, so the method returns nil. The bind is then sent without a type, and the client library infers one from the value. That works for a string but not for nil.

    Suggested fix

    Unwrap the enum to its underlying column type before the lookup, the same way DelegateClass types are unwrapped now:

    def self.convert_active_model_type_to_spanner type
      return nil if type.nil?
    
      type = type.__getobj__ if type.respond_to? :__getobj__
      type = type.subtype if type.is_a? ActiveRecord::Enum::EnumType
    
      TYPE_MAP[type.class] || fallback_convert_active_model_type_to_spanner(type)
    end
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the googleapis/ruby-spanner-activerecord API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions