-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different column types and enum issues #57
Comments
A workaround User.each_row do |attributes|
user = User.instantiate attributes
# do stuff
end Note: it only works for simple queries where returned columns map directly to model columns |
Looking at rails code, rails defaults column types instead of relying on postgres. while postgresql_cursor/lib/postgresql_cursor/cursor.rb Lines 229 to 249 in 6123a23
|
We had the same issue with an Our workaround was to use the cursor instance directly to override the klass param passed to each_instance: class ModelInstantiator
def initialize(model)
@model = model
end
def instantiate(row, column_types)
column_types = column_types.reject { |k, _| @model.attribute_types.key?(k) }
@model.instantiate(row, column_types)
end
end
SomeModel.all.each_row(**some_kwargs).each_instance(ModelInstantiator.new(SomeModel)) do |record|
# .... do something with record
end |
I have an issue with activerecord (= 7.0.5.1) |
Update: I endup following to what Rails has been doing with by rejecting if the attribute_types has the mapping keys def each_instance(klass = nil, &block)
klass ||= @type
each_tuple do |row|
if ::ActiveRecord::VERSION::MAJOR < 4
model = klass.send(:instantiate, row)
else
@column_types ||= column_types.reject { |k, _| klass.attribute_types.key?(k) }
model = klass.send(:instantiate, row, @column_types)
end
block.call(model)
end
end
def each_instance_batch(klass = nil, &block)
klass ||= @type
each_batch do |batch|
models = batch.map do |row|
if ::ActiveRecord::VERSION::MAJOR < 4
klass.send(:instantiate, row)
else
@column_types ||= column_types.reject { |k, _| klass.attribute_types.key?(k) }
klass.send(:instantiate, row, @column_types)
end
end
block.call(models)
end
end and it works |
each_instance
incorrectly sets the column types, for exampleExpected.
in the example above objects from both queries should be identical
rails -v => Rails 6.0.3.4
ruby -v => ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
The text was updated successfully, but these errors were encountered: