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.

Model.create() returns object without primary key

See original GitHub issue

Just 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:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
mickhansencommented, Aug 23, 2015

Does it work when you camelcase autoincrement as autoIncrement? Assuming the definition is a C/P that’s likely your issue.

4reactions
zanemccacommented, Jun 21, 2018

Sorry to bring this back up but I feel like this issue should not be closed. Setting autoIncrement: true seems like an irrelevant option that should not affect what is returned. Also, I feel like it is far to easy to forget that autoIncrement needs to be set which can result in unexpected side effects. These two factors makes me think that a better solution is definitely needed.

Read more comments on GitHub >

github_iconTop 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 >

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