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.

Custom ID on insert

See original GitHub issue

I am trying to automatically create custom generated ID’s, instead of auto incrementing every time I make an insert.

I have created a custom function that gets called on creation but do not see this change reflected and getting some strange errors (it thinks its an update not an insert). Is this the correct place to put my custom function.

Here is my model:

db = require('../db.coffee')

Proxy = db.Model.extend(
  tableName: "proxies"
  initialize: () ->
    this.on("creating", this.createId("prefix_"))
  hasTimestamps: ["date_created", "date_modified"]
)

Here is my insert:

new Proxy().save(name: request.payload.name).then (model) ->
    reply('success')

Any ideas on the best way to implement this?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bendruckercommented, Jan 30, 2014

Bookshelf determines whether a particular operation is an update or an insert based on whether it has an ID. (http://bookshelfjs.org/#Model-isNew) If you think about it, that makes sense—in most cases you won’t have an ID until you insert to the DB and the auto-incremented ID is returned. You can always override this behavior by expressly specifying a method in the save options, either insert or update.

new Proxy.save(data, {method: 'insert'});

It would help to see your createId method in order to diagnose why it’s not working. Keep in mind though that on expect an event handler, so unless createId returns a function, your code won’t work.

Unfortunately using a pre-defined ID means you have to manually specify any time you want to do an insert. There’s no way for Bookshelf to decide whether a model has been persisted or not if it can’t rely on the ID.

0reactions
eltonkcommented, Apr 29, 2016

Thanks for the answers… The problem happened when I tried to load the models using express-load. When I changed to use the ‘required old style’, everything works as expected.

Regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql server - Custom ID Numbering - fast way on insert?
I need to assign custom ID numbers for markers during loads. I can not use an identity field, because the ID numbers are...
Read more >
How to insert a record with custom id use Spring data jdbc?
The way to do that with Spring Data JDBC is to register a BeforeSaveEvent ApplicationListener that creates the id and sets it in...
Read more >
How can i do insertMany with custom _id field? - MongoDB
One way i can think of is looping through each element in the array and add _id field with the id i want,...
Read more >
Spring Data JDBC - How to use custom ID generation
Spring Data JDBC - How to use custom ID generation ... Another way to have your will with IDs is to make the...
Read more >
Insert with custom id - Strapi Community Forum
Hi there, I would like to create an entity with a specific id. ... Insert with custom id ... strapi.query('TABLENAME').model.forge({id, ...
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