get generated autoincremental ID from MySQL after insertion by jugglingdb?

105 Views Asked by At

I'm using compound library for nodejs,

The primary key is autoincremental, after insertion in db thru a model object, the object has the id as NULL, but in the database has the correct number, how to get this number after calling "save()"?

    var user = new User();
    user.name = Math.random();
    user.save();
    console.warn(user);//the user.id is null, not the number in db

ps: is there also a way to use custom primary key names in the model instead of always id?

1

There are 1 best solutions below

0
webmaster.skelton On

This is a rather old post, but in the event you are still looking for an answer;

Instead of using the synchronous save method I would suggest using the asynchronous create method.

For example:

var user = new User();
user.name = Math.random()

User.create(user, function (err, newUser) {
    console.log(newUser)
});

The returned "newUser" object will contain the newly created id.