Unable to set custom value in ID column on Mysql table

Viewed 43

I am trying to set a value to the user table ID column, I was able to auto increment the value with datatype as an integer without any problem. But I want to set a value I get from the request payload to the database id column, and I am not able to do that at first I thought it's because of auto-increment so I tried removing that as well but then the ID field became null even after have the payload passing the id value in it.

ID is a primary key field on the table

model/user.model.js

const tableNameVal = 'db_users';
module.exports = (sequelize, Sequelize) => {
const User = sequelize.define('User', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    email: {
        type: Sequelize.STRING(264),
        unique: true,
        allowNull: true
    },
    name: {
        type: Sequelize.STRING(264),
        allowNull: true
    },
    ...
    referred_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
            model: tableNameVal,
            key: 'id'
        }
    },
    ...
}, {
    tableName: tableNameVal,
    underscored: true
});
return User;};

Request Payload

{
id: 91056633,
name: tester,
mobile_number: 1236555862,
email: abc@abc.in,
is_mobile_verified: true }

Response after adding the rows with auto-increment

{
        "is_nick_name_editable": true,
        "is_email_verified": false,
        "is_mobile_verified": false,
        "is_user_blocked": false,
        "id": 746,
        "mobile_number": "1236555862",
        "updatedAt": "2021-07-15T07:53:50.581Z",
        "createdAt": "2021-07-15T07:53:50.581Z"
    }

Response after adding the rows without auto-increment

{
        "is_nick_name_editable": true,
        "gender": null,
        "is_email_verified": false,
        "is_mobile_verified": false,
        "is_user_blocked": false,
        "id": null,
        "mobile_number": "1236555863",
        "updatedAt": "2021-07-15T08:25:09.965Z",
        "createdAt": "2021-07-15T08:25:09.965Z"
    }
0 Answers
Related