[help] how to save model with given id
See original GitHub issueRequirement
We need to save a model with given id.
Question
What schema should I defined? And when save a mode, should I add id property.
Currently my code is:
// the schema
const { Nohm } = require("nohm");
Nohm.setPrefix("bus");
const Pet = Nohm.model("Pet", {
properties: {
id: { type: "string", index: true, unique: true }, // vin
ns: { type: "string", index: true }
}
});
const pet = new Pet();
// set some properties
pet.property({
id: "a-given-id-not-auto-generated",
ns: "/Gondor",
});
We find that model will be stored with an extra auto generated uuid in the key.
like
nohm:hash:Pet:c8792349-sdf234234-dsfs-234ssdfsf-234234
And when we load it
Pet.load("a-given-id-not-auto-generated");
it throw not found error
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
After using save function for an eloquent model I got a wrong ID
After I save using an eloquent model and I try to get an ID, the value of the ID is wrong. For example,...
Read more >Saving a model with one more key than just id - Laracasts
You can new up or declare a model. Set each column you want and end with save(). Which will override fillable/guarded. Or pass...
Read more >How to Save and Load Your Keras Deep Learning Model
You can save your model by calling the save() function on the model and specifying the filename. The example below demonstrates this by...
Read more >Calling `$model->save()` does not work if the `$model` object ...
To test the given issue, just open tinker and do the following, with any regular model that has a standard incrementing id:.
Read more >save() and .objects.create() does not set the primary key when ...
p = Person('name'=Bob) p.save() p.id. If however the model is missing the id field and django auto generates it: class Person(models.Model): name =...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’m not exactly sure what you mean.
Option 1
You want some other method than uuid or incremental ids for all your ids for a model.
This is covered by idGenerators.
Option 2
You have some specific (and rare) cases where you don’t want the default generation to happen when creating new instances and you already have an ID that you want to use for it and you are either sure it doesn’t already exist or you are fine with updating an old one if it does exist.
Again: WARNING! You are completely responsible for the ids this way and have to make sure that when you create a new instance the id you pass is available at the time of saving the object! For example the built-in incremental id generator increments before saving. Thus no 2 saves can ever have the same id, no matter how concurrent. It has the downside that if something goes very wrong, it may skip numbers, but that’s more acceptable than accidentally updating when you want to create.
If you don’t set the id before saving a new instance, it will fall back to creating a uuid.
@maritz
Thank you. option 2 is exactly what we need.
We use Redis to save some data which comes from other services. These modes will have ids, and are used in api call, like
/pets/:petId.