SamuelP
SamuelP

Reputation: 195

How Do I Fix this Sequelize Database Error when trying to do a POST method?

I am using Postman to test a POST route for my React application using Sequelize, node and express. I am getting this error below

{
    "name": "SequelizeDatabaseError",
    "parent": {
        "code": "ER_NO_DEFAULT_FOR_FIELD",
        "errno": 1364,
        "sqlState": "HY000",
        "sqlMessage": "Field 'title' doesn't have a default value",
        "sql": "INSERT INTO `Trips` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);",
        "parameters": [
            "2019-12-01 00:50:42",
            "2019-12-01 00:50:42"
        ]
    },
    "original": {
        "code": "ER_NO_DEFAULT_FOR_FIELD",
        "errno": 1364,
        "sqlState": "HY000",
        "sqlMessage": "Field 'title' doesn't have a default value",
        "sql": "INSERT INTO `Trips` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);",
        "parameters": [
            "2019-12-01 00:50:42",
            "2019-12-01 00:50:42"
        ]
    },
    "sql": "INSERT INTO `Trips` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);",
    "parameters": [
        "2019-12-01 00:50:42",
        "2019-12-01 00:50:42"
    ]
}

The schema for my table is as follows

CREATE TABLE Trips (
  id INT NOT NULL AUTO_INCREMENT,
  title varchar(255) NOT NULL,
  location varchar(255) DEFAULT NULL,
  Description varchar(255) DEFAULT NULL,
  tripDate datetime DEFAULT NULL,
  image varchar(255) DEFAULT NULL,
  createdAt timestamp default current_timestamp,
  updatedAt timestamp,
  PRIMARY KEY (id)
);

I tried changing the various columns to be DEFAULT NULL but even when I input data into those fields, I am getting back null in the database. I added images of my code.

React Form React Form

Trips Controller Trips Controller

Trips Model Trips Model

Trips Router Trips Router

-Sam

Upvotes: 4

Views: 15968

Answers (2)

Naor Levi
Naor Levi

Reputation: 1813

You have 2 problems:

1) At your SQL declaration and in your model declaration you're missing the default value for the title column.

Your SQL table declaration should like this:

CREATE TABLE Trips (
  id INT NOT NULL AUTO_INCREMENT,
  title varchar(255) DEFAULT NULL, -- Or any other default value
  location varchar(255) DEFAULT NULL,
  Description varchar(255) DEFAULT NULL,
  tripDate datetime DEFAULT NULL,
  image varchar(255) DEFAULT NULL,
  createdAt timestamp default current_timestamp,
  updatedAt timestamp,
  PRIMARY KEY (id)
);

According to this declaration your model declaration should be:

const Trips = sequelize.define('Trips', {
    title: { 
             type: DataTypes.STRING,
             defaultValue: null // or whatever you would like
    },
    location:DataTypes.STRING,
    Description:DataTypes.STRING,
    tripDate:DataTypes.DATEONLY,
    image:DataTypes.STRING
  },

If you still get this error after modifiying these two your problem is on the client side, for some reason the title data doesn't pass to the server and therefore is undefined at req.body

Upvotes: 2

ashwani bakshi
ashwani bakshi

Reputation: 66

1.by default it will look for createdAt,updatedAt in your model schema. so either you add the createdAt,updatedAt else if you don't want these 2 fields then set timestamps:false in model schema.

2.you should add id field in your model schema because sequelize always require a id field with primary key in your model schema.Its necessary in every model.

 const Trips = sequelize.define('Trips',{
id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: Sequelize.INTEGER
  },
  title:DataTypes.STRING,
  location:DataTypes.STRING,
  Description:DataTypes.STRING,
  tripDate:DataTypes.DATEONLY,
  image:DataTypes.STRING
  },
 {timestamps:false});

Upvotes: 1

Related Questions