question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[help] how to save model with given id

See original GitHub issue

Requirement

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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
maritzcommented, Jul 7, 2019

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.

const Pet = Nohm.model("Pet", {
  properties: {
    ns: { type: "string", index: true }
  }
});

const pet = new Pet();

pet.id = 'foo-bar';
pet.property({
  ns: "/Gondor",
});

await pet.save();

await Pet.load('foo-bar'); // should return the pet we just created.

// HOWEVER!
// if you then do this:

const pet2 = new Pet();

pet2.id = 'foo-bar';
pet2.property({
  ns: "/Mordor",
});

await pet2.save(); // this UPDATES the "pet1" we created earlier, because the id is already in the database.

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.

2reactions
zzswangcommented, Jul 9, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found