NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

Model.insert_all and generated keys #179

Closed alolis closed 8 years ago

alolis commented 8 years ago

Hello,

I am using latest version of nobrainer gem with RethinkDB 2.1.4 and suddenly when I execute model.insert_all I do not get the returned keys that were generated. I debugged nobrainer a little bit and it seems that the problem is here:

NoBrainer::Document::Persistance line 144
 def insert_all(*args)
      docs = args.shift
      docs = [docs] unless docs.is_a?(Array)
      docs = docs.map { |doc| persistable_attributes(doc) }
      result = NoBrainer.run(rql_table.insert(docs, *args))
      result['generated_keys'].to_a
  end

result hash does not have a generated_keys key; it only contains deleted, errors, inserted, replaced, skipped and unchanged

Any ideas where the generated_keys field went?

nviennot commented 8 years ago

Can you provide a standalone working example that exhibit the bug? Thank you :)

alolis commented 8 years ago

@nviennot , It seems that I will answer my own question. The reason that generated_keys is missing is because I append custom IDs to those documents. I tested without appending custom IDs and it is working fine.

Model.insert_all however, will return the generated_keys field, which in my case does not exist and as a result is hard for me to implement error handling. In my case the inserted field is more appropriate. Is there a way to get the whole result or insert_all must be changed first?

nviennot commented 8 years ago

You might want to use raw RQL. You may do something like this:

results = NoBrainer.run { Model.rql_table.insert(documents) }
alolis commented 8 years ago

@nviennot , cheers, i will give it a go

@nvedul2 , it's not polite to try to hijack another persons thread.