Custom ID on insert
See original GitHub issueI 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:
- Created 10 years ago
- Comments:9 (6 by maintainers)
Top 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 >
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
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
orupdate
.It would help to see your
createId
method in order to diagnose why it’s not working. Keep in mind though thaton
expect an event handler, so unlesscreateId
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.
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