upsert should return auto-generated id of newly created model instance
Description/Steps to reproduce
I have a some models generated from postgresql db using looback-connector postgresql. Id column of these models is a auto incremented integer column of postgresql db.
- I have a remote method added on one of persisted models, where i perform simple update or insert(upsert).
Car.CreateOrUpdateCar = function (carobj, req) {
Car.upsert(Carobj, function (err, Car) {
if (err)
console.log(err);
else {
req(err, Car);
}
});
};
- have added a remote hook to execute after this remote method.
Car.afterRemote('CreateOrUpdateCar', function (context, remoteMethodOutput, next) { //Remaining code goes here next(); }); - I want to use Id of newly inserted row in step (1), in the remote hook mentioned in step (2)
Expected result
I should get complete object of newly created record, including id generated by auto increment column in posgresql db.
Instaed of that, i get object same as it was passed by me to upsert. As i am creating a new record, i can specify id as {"id":null} or do not include id at all. But when i pass it as null, returned object also has id as null. If i dont invlude Id key in inserting object, returned object by upsert also dosent have id.
Additional information
This is how my id field is defined in model "id": { "type": "Number", "id": true, "required": false, "index": true, "length": null, "precision": 32, "scale": 0, "generated": true, "postgresql": { "columnName": "Id", "dataType": "integer", "dataLength": null, "dataPrecision": 32, "dataScale": 0, "nullable": "NO" }
Hello, as I mentioned on Gitter, I think this is a missing feature of LoopBack or the PostgreSQL connector.
The first place where to start looking is lib/dao.js in loopback-datasource-juggler.
For example, the create method invoked on the connector returns id and rev properties - see
https://github.com/strongloop/loopback-datasource-juggler/blob/76c73d55d28dfa150b9e004aeae12aafe0ce6475/lib/dao.js#L359
OTOH, the upsert method returns a full data object - see https://github.com/strongloop/loopback-datasource-juggler/blob/76c73d55d28dfa150b9e004aeae12aafe0ce6475/lib/dao.js#L596
I am not sure what are the expectations about this data object parameter. The built-in memory connector does update the id value if it was auto-generated, see https://github.com/strongloop/loopback-datasource-juggler/blob/76c73d55d28dfa150b9e004aeae12aafe0ce6475/lib/connectors/memory.js#L254
Here is what I think we should do:
- Add a new test to test/manipulation.test.js, where we upsert a new record with auto-generated
idand verify that the response includes the generated id. - When a pull request is opened with this new test, our CI system will run the new test for all connectors we are maintaining at IBM/StrongLoop. As a result, we should see which connectors are not honouring the expectation that
updateOrCreateshould return the auto-generated id. - Based on the outcome of the previous step, we can look for the best place where to apply the necessary fix(es). I think we may be able to fix this for all SQL-based connectors in
loopback-connectormodule.
Hi @bajtos , I tried for above changes but get struck on below point,
var modelName = Model.modelName;
we are getting model name in string format, how can I convert it into Object on which i can perform operation like as,
modelName.find({
order: 'price DESC',
limit: 3
});
Hi @bajtos Thank you for bringing it on forum, Encountered similar issue as that of @chetanmekha . This feature is so crucial as it stops me from doing map table inserts. Is there any other option or work around? so that it doesn't become a deadlock. Can we invite more contributors who can manage to do this fix?
@grmahajan11 @chetanmekha could you please post the code showing what you have tried? It's difficult for me to help you without that.
This feature is so crucial as it stops me from doing map table inserts. Is there any other option or work around? so that it doesn't become a deadlock.
I don't know any, perhaps @raymondfeng or @strongloop/sq-lb-apex may be aware of some? (The question is how to make upsert return auto-generated id of newly created model instances when using PostgreSQL connector. AFAICT, the memory connector does include auto-generated id in the response.)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
I am facing the same problem for the create method. When a record is inserted, the auto generated ID is not returned. @grmahajan11, did you find a solution to this ?
I encountered the same and was able to resolve by overriding the PostgreSQL.prototype.getInsertedId - the column name was lowercase but my model attribute name was uppercase so it couldn't extract the ID from the result object.
There's a strange case issue with postgreSQL that I've not encountered with Oracle and MS SQL.
I face the same issue. If I rename the autogenerated identifier from "id" to "Id", the identifier does not get returned. If I rename it back to lowercase "id", then it works 😞
Hope someone is fixing this. This issue is haunting from 4 years now I guess https://stackoverflow.com/questions/38138456/insert-or-updateupsert-in-loopback-application