dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

How to insert multiple records Node orm2 #765

Closed apmeena closed 7 years ago

apmeena commented 7 years ago

http://stackoverflow.com/questions/41761582/how-to-insert-multiple-records-using-node-orm2

dxg commented 7 years ago

This is not a great way to post issues as if the stack overflow link stops working, this post will not help anyone else reading this. https://github.com/dresende/node-orm2/blob/2fef6a9791443416fc6255bd286d037a77f5034c/test/integration/model-create.js#L54

apmeena commented 7 years ago

@dxg Now I got the solution, the mistake I was doing is I was generating the data using a for loop like this:

var data = { name:'john', age:24, email:'abc@abcd.com'};

var arrayOfObjects= [];

for (var i = 0; i < 4; i++){
   arrayOfObjects.push(data);
}

modelObj.create(arrayOfObjects, function (err, result) {
      if (err) {
         console.log("The error is :", err);
      }
      else {
         response.status = 'success';
         response.data = result;
      }
    next(response);
});

So each and every record is same in this condition. However other than id none column having the property primary-key. I think it can be the behavior of the database or the node-orm2 can also be the reason, so it is not accepting the exact same values.

In actual, all the record won't be same. If you have some other point please let me know your thoughts. Thanks.