how i can fix the SequelizeDatabaseError: Field 'id' doesn't have a default value?

12,292

As per my understanding this is a problem with mysql database. The 'id' field in your table is marked as not null, and while inserting values you are not providing any value for the 'id' field.

You can avoid facing this error either allowing null values in the 'id' field or providing a default value (You can use defaultValue flag) while creating the table. Otherwise you already got an alternate solution.

Share:
12,292

Related videos on Youtube

pablo calofatti
Author by

pablo calofatti

Updated on June 04, 2022

Comments

  • pablo calofatti
    pablo calofatti almost 2 years

    im working on a To do list in node using MVC node express pug and sequelize as ORM and the views part i made it with pug, so when i start my app works ok but when i try to create a new proyect and this proyect its gone to insert in the db the error 1364 appears

    in principle it is a personal project to add to my portfolio, I saw that there is a possible solution by deactivating the STRICT SQL mode but I would like to know if there is another solution

    model code Proyect.js
    
    const Sequelize = require('sequelize');
    //conect db
    const db = require('../config/db');
    
    const Proyects = db.define('proyects', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false
           },
        name: Sequelize.STRING,
        url: Sequelize.STRING
         });
    module.exports = Proyects;
    

    I expect with this model create a register in my table but when I try the actual error its the next

    Executing (default): CREATE TABLE IF NOT EXISTS `proyects` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `url` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
    Executing (default): SHOW INDEX FROM `proyects`
    conected to db
    Executing (default): INSERT INTO `proyects` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);
    { SequelizeDatabaseError: Field 'id' doesn't have a default value
      code: 'ER_NO_DEFAULT_FOR_FIELD',
         errno: 1364,
         sqlState: 'HY000',
         sqlMessage: 'Field \'id\' doesn\'t have a default value',
         sql:
          'INSERT INTO `proyects` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);' },
    
    • Cees Timmerman
      Cees Timmerman almost 4 years
      Welcome to Stack Overflow. If there is a working answer to your question, please mark it as such.