clickhouse-activerecord icon indicating copy to clipboard operation
clickhouse-activerecord copied to clipboard

Problem with Nullable Int data (is out of range for ActiveModel::Type::Integer with limit 0 bytes)

Open galievruslan opened this issue 3 years ago • 4 comments

Hello I use ruby-3.1.2, rails 7.0.3 and clickhouse-activerecord gem version 0.5.8

My migration:

def up
    execute <<~SQL
      CREATE TABLE if not exists events
      (
        value_int Nullable(Int32),
        created_at DateTime,
        updated_at DateTime
      )
      engine = Log
    SQL
  end

  def down
    execute <<~SQL
    drop table events
    SQL
  end

On console I tried to creating new record:

3.1.2 :003 > Event.create(value_int: 10)
/home/ruslan/.rvm/gems/ruby-3.1.2/gems/activemodel-7.0.3/lib/active_model/type/integer.rb:52:in `ensure_in_range': 10 is out of range for ActiveModel::Type::Integer with limit 0 bytes (ActiveModel::RangeError)

How resolve this problem?

galievruslan avatar Jun 20 '22 09:06 galievruslan

I try create empty project using rails 6.1.6 . Create simple model with one integer not null (Nullable) field. Everything works without problems. Problem with relation rails 7.0.3.

galievruslan avatar Jun 20 '22 15:06 galievruslan

ah, same problem, rails 7.0.3

1c7 avatar Aug 05 '22 15:08 1c7

I fix the problem by changing gem 'activerecord', '7.0.3' to gem 'activerecord', '6.1.6' (it's not a rails project. I am using activerecord directly to connect PostgreSQL + ClickHouse to do some data analytic work. read some data from PostgreSQL and put the result into ClickHouse)

1c7 avatar Aug 05 '22 15:08 1c7

In rails 7, connection adapter methods :extract_limit, :extract_scale, :extract_precision, :initialize_type_map should be a class methods instead of instance ones. Creating explicit type map like

class ClickhouseAdapter < AbstractAdapter
  ...
  class << self
    def extract_limit
      ...
    end
    def extract_scale
      ...
    end
    def extract_precision
      ...
    end
    def initialize_type_map(m)
      ...
    end
  end

  CH_TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map(m) }
  def type_map = CH_TYPE_MAP
end

Solves the issue at the first glance. Will check it out in details

orlando-labs avatar Aug 10 '22 14:08 orlando-labs