Model.create() returns object without primary key
See original GitHub issueJust as a reference, this issue is very similar to the one described in #1060, but different enough that I felt it deserved its own issue.
If I define a custom primary key column for the table (instead of the default generated id column added when building the model with sequelize-cli model:create), the object passed back in the response has a null value for the primary key. The real kicker is that the saved object that comes back in the response does come back with the pk, but not on the custom pk column. The value only exists on a property called null.
Person model:
module.exports = function(sequelize, DataTypes) {
var Person = sequelize.define('Person', {
personId: {
autoincrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
gender: DataTypes.STRING,
home: DataTypes.STRING
});
return Person;
};
Migration file:
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('People', {
personId: {
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
gender: {
type: Sequelize.STRING
},
home: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('People');
}
};
And the saved Person object
{
dataValues:
{
personId: null,
firstName: 'Wade',
lastName: 'Wilson',
gender: '0',
home: '5555555555',
updatedAt: Fri Aug 21 2015 15:23:26 GMT-0600 (MDT),
createdAt: Fri Aug 21 2015 15:23:26 GMT-0600 (MDT)
},
// many other properties omitted here for readability's sake
isNewRecord: false,
null: 3
}
My route:
var express = require('express');
var router = express.Router();
var models = require('../../models');
var Sequelize = require('sequelize');
var Person = models.Person;
router.post('/create', function(req, res) {
Person
.create(req.body.person)
.then(function(savedPerson) {
console.log( savedPerson.null ); //// <= This line prints out "3".
});
});
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Model create returns object without ID - laravel - Stack Overflow
The table has ID and some fillable fields. It's called inside a transaction. The row is actually created in the database, but after...
Read more >15304 (`Model.objects.create()` returns `long` instead of `int`.)
The primary key value returned by Model.objects.create() and Model.objects.get() should be the same. It seems to be a clear bug that the primary...
Read more >Working with Legacy Tables - Sequelize
Instances without primary keys can still be retrieved using Model.findOne and Model.findAll . While it's currently possible to use their instance methods ( ......
Read more >Models - Tortoise ORM v0.19.3 Documentation
In Tortoise ORM we require that a model has a primary key. ... Create a new clone of the object that when you...
Read more >Keys - EF Core | Microsoft Learn
For non-composite numeric and GUID primary keys, EF Core sets up value generation for you by convention. For example, a numeric primary key...
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

Does it work when you camelcase
autoincrementasautoIncrement? Assuming the definition is a C/P that’s likely your issue.Sorry to bring this back up but I feel like this issue should not be closed. Setting
autoIncrement: trueseems like an irrelevant option that should not affect what is returned. Also, I feel like it is far to easy to forget thatautoIncrementneeds to be set which can result in unexpected side effects. These two factors makes me think that a better solution is definitely needed.