Using MongoDB
POSTing this
{
"name": "someName",
"subject": "Foo",
"body": "Bar",
"id": "SomeTerribleIdIDontWant"
}
Will happily insert the record, including the id, into the collection. This happens even if I don't have an "id" attribute listed in my model.
How do I force a database-generated id when inserting a new record?
if you don't provide primary key, By default Strongloop use id attribute as primary key.
I'm fine with that. I just don't want to let whoever is running the POST request get to decide what the id of the new record is. I want the database to always come up with the id. This is done correctly if you don't pass in the "id" attribute in the request body, but if you do pass it in the database will use it as the id
Set variable forceId: true in model definition.
Il giorno lun 15 feb 2016 15:49 Andrew Roth [email protected] ha
scritto:
I'm fine with that. I just don't want to let whoever is running the POST
request get to decide what the id of the new record is. I want the database
to always come up with the id. This is done correctly if you don't pass in
the "id" attribute in the request body, but if you do pass it in the
database will use it as the id—
Reply to this email directly or view it on GitHub
https://github.com/strongloop/loopback/issues/2068#issuecomment-184236907
.
The only documentation I'm able to find for forceID is in the relations section.
I finally got it to work by adding an "_id" attribute
"_id":{
"type":"string",
"id":true,
"generated":true
}
and putting this in my model's .js file
module.exports = function(yourModelNameHere) {
yourModelNameHere.validatesAbsenceOf("_id");
};
@mrbatista's recommendation is what I would do as well!
If you want to prevent someone to set this auto-id value manually, set forceId: true. The only documentation I could find is here which explains forceid briefly. @crandmck is the best person for documentation; do we need more explanation about this in documentation?
Thanks!
Model definition, not property definition.
Il giorno lun 15 feb 2016 20:04 Andrew Roth [email protected] ha
scritto:
Do I put forceID in the JSON object for the "_id" attribute or the model
itself?—
Reply to this email directly or view it on GitHub
https://github.com/strongloop/loopback/issues/2068#issuecomment-184349017
.
At a minimum, we should include this information in the model JSON file reference. It's missing any explanation of the relations.<modelName>.options property (and its constituents), and also appears to be missing the relations.<modelName>.property option.
You can't edit or use the endpoint replaceOrCreate if the forceId is true :/
You can't edit or use the endpoint replaceOrCreate if the forceId is true :/
Yes that is an expected behaviour; replaceOrCreate uses the PK and if you use forceId: true it means we are preventing users from passing the PK so in this case there would be no replace operation and it would be always create operation which contradicts with the end goal of replaceOrCreate.
@Amir-61 If we need to add or change something in http://loopback.io/doc/en/lb2/Model-definition-JSON-file.html, please open a PR.... or please give me a clearer idea of what we need to do.
@crandmck I'll find some time and send a PR ASAP.
Thanks!
Now forceId behaviour is added for both LB 2.x and LB 3.x in http://loopback.io/doc/en/lb2/Model-definition-JSON-file.html and http://loopback.io/doc/en/lb3/Model-definition-JSON-file.html respectively. The difference is default value where in LB2.x by default forceId is set to false whereas in LB 3.x forceId is set to true for security reason. Related to https://github.com/strongloop/loopback-datasource-juggler/pull/982
Is this still valid? I'm running loopback 3.8. I have tried putting "forceId": false, in the model definition both in the top level (as per the docs) and under "options", but still an update request from within the code would return Error: id cannot be updated from function () { [native code] } to 123 when forceId is set to true
In a different note, I am trying to perform a partial update to a record that I know the specific id for.
updateAttributes is not a functionField 'xxx' doesn't have a default valueYour help is highly appreciated.
@Amir-61 @mrbatista
@jannyHou PTAL ^
@shaheero +1.
I have "forceId": false in my model definition to prevent
ValidationError: The ProductCategory instance is not valid. Details:idcan't be set (value: 1)
I tried changing this behavior using before save hook too, but it seems to throw similar error...
Error: Duplicate entry for ProductCategory.id
at Memory._createSync (/projects/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:259:17)
at Memory.create (/projects/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:270:8)
at invokeConnectorMethod (/projects/node_modules/loopback-datasource-juggler/lib/dao.js:169:21)
at /projects/node_modules/loopback-datasource-juggler/lib/dao.js:472:11
at doNotify (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
at doNotify (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
at doNotify (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
at doNotify (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
at Function.ObserverMixin._notifyBaseObservers (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:178:5)
at Function.ObserverMixin.notifyObserversOf (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (/projects/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)
My goal is to allow users to use POST for both create/update. I understand, its a bad API design as per REST but unfortunately required to comply to this standard as other APIs are designed this way.
Did you have any solution for this? still present
sql:
'UPDATEgroupSETadmin=\'function () { [native code] }\' WHEREid=129',
Most helpful comment
Is this still valid? I'm running loopback 3.8. I have tried putting
"forceId": false,in the model definition both in the top level (as per the docs) and under"options", but still an update request from within the code would returnError: id cannot be updated from function () { [native code] } to 123 when forceId is set to trueIn a different note, I am trying to perform a partial update to a record that I know the specific id for.
updateAttributes is not a functionField 'xxx' doesn't have a default valueYour help is highly appreciated.
@Amir-61 @mrbatista