Вопрос

I'd like to get the ID of a project before saving in database... I know that the ID is generated during the saving but is there a solution?

var photo = dbContext.photo.build(photo);
console.log(photo.id); // doesn't work
photo.save().success(function(photo) {
    console.log(photo.id); // works
    callback(photo);
});
Это было полезно?

Решение

Theoretically, you can query for the next id, generated by auto increment, see this How to get the next auto-increment id in mysql

However, remember that you have to do this within a transaction, where you lock the table before you query it, to prevent other processes from inserting and taking up the id that you just got. Have a look at https://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

However, this is going to get really, really complicated really, really quickly. Are you sure there is no other way to solve this problem? Can't you update the name after the record has been saved, that would be tremendously more easy?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top