Skip to content
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

Result returned from INSERT is nil #41

Closed
bradrf opened this issue Oct 11, 2018 · 9 comments
Closed

Result returned from INSERT is nil #41

bradrf opened this issue Oct 11, 2018 · 9 comments

Comments

@bradrf
Copy link

bradrf commented Oct 11, 2018

Upgrading from v1.6.0 to v1.7.0, I'm seeing the following failure when trying to call save! on a bulk worker (using mysql2 v0.4.5 database connector against mysql v5.7.23 server):

D, [2018-10-11T09:17:07.739444 #72035] DEBUG -- : 2018-10-11 09:17:07.734938 D [72035:70195788170560] (0.727ms) ActiveRecord::Base -- SQL -- {:sql=>"INSERT  INTO `recent_files` (`repo_id`,`md5`,`size`,`created_at`,`updated_at`) VALUES (1,'ae3e83e2fab3a7d8683d8eefabd1e74d',3,'2018-10-11 16:17:07','2018-10-11 16:17:07')"}
E, [2018-10-11T09:17:07.740832 #72035] ERROR -- : 2018-10-11 09:17:07.735079 E [72035:70195788170560 bulk_insert_thread.rb:29] BulkInsertThread -- Exception: NoMethodError: undefined method `fields' for nil:NilClass
.../gems/activerecord-4.2.10/lib/active_record/connection_adapters/mysql2_adapter.rb:222:in `exec_query'
.../gems/bulk_insert-1.7.0/lib/bulk_insert/worker.rb:92:in `execute_query'
.../gems/bulk_insert-1.7.0/lib/bulk_insert/worker.rb:82:in `save!'
.../lib/modules/bulk_insert_thread.rb:27:in `block in initialize'
@philister
Copy link

+1

@mberlanda
Copy link
Collaborator

mberlanda commented Jan 14, 2019

hey @bradrf @bradrf could you provide a snippet to reproduce the issue?
E.g.

  • table creation migration
  • buik insert statement

https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb#L220-L223

@rfbezerra
Copy link

I have the same issue. Seems to be related to older versions of ActiveRecrod (4.2.10 is mine).

def exec_query(sql, name = 'SQL', binds = [])
  result = execute(sql, name)
  ActiveRecord::Result.new(result.fields, result.to_a)
end

The problem is that, the return of execute method is nil, so when result.fields is accessed throws error
When I have time, Ill investigate if there is a way to return the ids.

Maybe it occurrs because of sql nature, with multiple values in single query

@waynerobinson
Copy link

We've just run into this issue when attempting to add this library to our project.

It's rails 4.2.11.3 and mysql2 0.5.3.

@darthwiz
Copy link

I have the same issue with activerecord 3.2.22.2 and mysql2 0.3.21 - very old application, sorry.

Taking a hint from the original post I tried to downgrade bulk_insert to commit 4313c04 (1.6.0 with AR 3.2 compatibility) but that didn't solve the problem.

@mberlanda
Copy link
Collaborator

hi @darthwiz , is your project open source? As of today AR 3 compatibility should not be broken but there is no regression test. I am reviving this old PR to make sure that I put some regression tests for future contributions #61 (it would be great eventually to have the reference of a sample rails 3 application to add to the test cases)

@rfbezerra @waynerobinson would you able to provide a patch working with your active record version?
I will be happy to address your issue

@mberlanda
Copy link
Collaborator

The main difference I can see in this diff v1.6.0...v1.7.0 is calling @connection.exec_query instead of @connection.execute.

If I remember correctly, this was done to support returning primary keys for postgresql connections #32 .

We can eventually restore the previous behaviour under some special conditions: e.g. when AR < 5 and the connection is mysql.
What do you think?

@mberlanda
Copy link
Collaborator

In #77 I added some unit tests using mysql adapter.
I could reproduce the issue you were describing with tests allowed to fail:

  1) Error:
BulkInsertWorkerTest#test_explicit_nil_should_override_defaults:
NoMethodError: undefined method `fields' for nil:NilClass
    /home/travis/.rvm/gems/ruby-2.3.8/gems/activerecord-4.2.11.3/lib/active_record/connection_adapters/mysql2_adapter.rb:222:in `exec_query'
    /home/travis/build/jamis/bulk_insert/lib/bulk_insert/worker.rb:96:in `execute_query'
    /home/travis/build/jamis/bulk_insert/lib/bulk_insert/worker.rb:86:in `save!'
    /home/travis/build/jamis/bulk_insert/test/bulk_insert/worker_test.rb:67:in `block in <class:BulkInsertWorkerTest>'

The test is failing both for rails 3 and 4.
I am going to merge this testing pr as it is and fix the issue in a follow up pr

@mberlanda
Copy link
Collaborator

This issue was resolved in release 1.9.0.

Please feel free to confirm after the gem update and to re-open the issue if you still face the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants