balderdashy / waterline-docs

WARNING: The content in this repo is out of date! See https://github.com/balderdashy/sails-docs for the most up-to-date documentation
452 stars 163 forks source link

Clarification of accepted data types #67

Open mattrat opened 9 years ago

mattrat commented 9 years ago

While trying to create and/or update multiple records with one call, it was unclear if each of the accepted data types can be used together. Example. When passing and array of object ids as the first parameter and a array of objects with new attribute data to update, what is the resulting action.

"Questions.update( toUpdateObjectIDs , newQuestionsAttribs )"

After test for a while, I could not get this to work. If this is supposed to work, ie can you give an array of objects for the first parameter and the second parameter. Should this update each of first arrays objects with the corresponding arrays attributes? Or does is try to update each of the first arrays objects with each (and every) of the attributes of the second array.

An example would also be incredibly helpful. Thanks.

dmarcelino commented 9 years ago

So, if I understand correctly your question is about .update() with both criteria and values using arrays, example:

var toUpdateObjectIDs = [1, 2];
var newQuestionsAttribs = [{ question: 'name?' }, { question: 'age?' }];
Questions.update(toUpdateObjectIDs, newQuestionsAttribs)

Right? In that case I would expect to see the following results:

id question
1 'name?'
2 'age?'

Are you getting something different? I couldn't find this particular scenario on the waterline-adapter-tests and a PR would be more than welcomed!

@devinivy, have you ever used .update([], [])?

devinivy commented 9 years ago

I've not! A good place to start looking for the functionality is here (the collection method), here (the adapter-calling method), or in the specific adapter. Sorry if that wasn't incredibly helpful, but I'm not sure off the top of my head if waterline or the particular adapter is responsible for accepting Array types.

dmarcelino commented 9 years ago

Thanks, those are good references. I couldn't find this scenario in the tests, I wonder if this was implemented...

mattrat commented 9 years ago

@devinivy - Thanks for the references they are really helpful.

@dmarcelino - Your scenario is correct. That is what I am trying to accomplish but it does not update. In fact, it fails. I also didn't see this scenario in the tests.

dmarcelino commented 9 years ago

In fact, it fails.

can you post the error you get?

mattrat commented 9 years ago

Here is the code I'm using and the top lines of the error. I have been putting in a work around and will have to pull some things out to give the rest of the error message.

var toUpdate = [ { q_num: 1 },{ q_num: 2 } ]; var ques = [ { q_num: 1, text_a: 'this', text_b: 'that' }, { q_num: 2, text_a: 'and', text_b: 'the other' } ];

Temperament_Questions.update(toUpdate,ques).exec(function(error, updates){ if (error){ console.log("error: "); console.log(error); }else{ console.log("Success"); } });

error: Error (E_UNKNOWN) :: Encountered an unexpected error : ER_BAD_FIELD_ERROR: Unknown column 'temperament_questions.[object Object]' in 'where clause' ...

devinivy commented 9 years ago

At first glance, looks like it's not implemented, and there were no assertions to catch it!

mattrat commented 9 years ago

Gotcha. I was able to work around with q.promises.allSettled(). Definitely would be a good feature to have though.

Thanks for helping.

dmarcelino commented 9 years ago

I guess this should be implemented following similar logic as createEach:

@mattrat, do you feel like contributing some code and tests to waterline? :smiley:

mattrat commented 9 years ago

Sure, sounds like fun. I have a couple week back log I'm trying to clear but as soon as I can I would love to help.