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

why import do not return imported ids, when it called in sidekiq worker ? #793

Open
Asdfif opened this issue Feb 13, 2023 · 6 comments
Open

Comments

@Asdfif
Copy link

Asdfif commented Feb 13, 2023

I use activerecord-import in Sidekiq worker and if I call worker by .perform_async then I get this result:
#<struct ActiveRecord::Import::Result failed_instances=[], num_inserts=1, ids=[], results=[]>

but if I call worker by .new.perform then I get this result:
#<struct ActiveRecord::Import::Result failed_instances=[], num_inserts=3, ids=[375, 376, 377], results=[]>

why?

@jkowens
Copy link
Collaborator

jkowens commented Feb 15, 2023

If you can share the sql that is being executed in each scenario we may be able to help. Right now there isn't enough information. Is it the same model/table that you are importing into? You must be using Postgres?

@darnat
Copy link

darnat commented Mar 2, 2023

Actually facing a very similar issue. I have an import running in Sidekiq and no ids are being returned. This case scenarios is covered by specs, and the specs are passing (it does return ids). It just doesn't work when running in production/sidekiq.

@Asdfif
Copy link
Author

Asdfif commented Mar 3, 2023

@jkowens I use MySQL.

sql query with sidekiq worker perform_async:

result = ModelName.import array, validate: false
D, [2023-03-03T10:17:51.725232 #64421] DEBUG -- :   ModelName Create Many (120.5ms)  INSERT INTO `table_name` (`id`,`prefix`,....) VALUES (NULL,'[email protected]',.....),(NULL,'[email protected]',....),(NULL,'[email protected]',...)
#<struct ActiveRecord::Import::Result failed_instances=[], num_inserts=1, ids=[], results=[]>

sql with sidekiq worker new.perform:

result = ModelName.import array, validate: false
   (0.6ms)  BEGIN
   (2.1ms)  INSERT INTO `table_name` (`id`,`prefix`,...) VALUES (NULL,'[email protected]',...)
   (1.3ms)  INSERT INTO `table_name` (`id`,`prefix`,....) VALUES (NULL,'[email protected]',)
   (3.2ms)  INSERT INTO `table_name` (`id`,`prefix`,....) VALUES (NULL,'[email protected]',....)
   (2.0ms)  COMMIT
#<struct ActiveRecord::Import::Result failed_instances=[], num_inserts=3, ids=[412, 413, 414], results=[]>

@jkowens
Copy link
Collaborator

jkowens commented Mar 3, 2023

@Asdfif Returning ids is not supported for MySQL. Unlike Postgres, MySQL does not provide a way to return the ids of inserted rows.

The example you show with the imported ids being populated doesn't seem right. Why did it produce 3 insert queries?

@Asdfif
Copy link
Author

Asdfif commented Mar 3, 2023

The example you show with the imported ids being populated doesn't seem right. Why did it produce 3 insert queries?

@jkowens I did not understand yet why there are 3 queries. trying to figure it out

@Asdfif
Copy link
Author

Asdfif commented Mar 3, 2023

Returning ids is not supported for MySQL. Unlike Postgres, MySQL does not provide a way to return the ids of inserted rows.

If MySQL does not provide a way to return the ids of inserted why it returns ids when i use new.perform...

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

3 participants