Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ngozistephen's avatar

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: bookings.start_date

I am getting this error message in my test. How can i solve this error

 SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: bookings.start_date (Connection: sqlite, SQL: insert into "booking
s" ("apartment_id", "user_id", "total_price", "updated_at", "created_at") values (1, 2, 0, 2023-05-29 02:23:05, 2023-05-29 02:23:05))

This is my migration file for booking

  public function up(): void
    {
        Schema::create('bookings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('apartment_id')->constrained();
            $table->foreignId('user_id')->constrained();
            $table->date('start_date');
            $table->date('end_date');
            $table->unsignedInteger('guests_adults');
            $table->unsignedInteger('guests_children');
            $table->unsignedInteger('total_price');
            $table->timestamps();
            $table->softDeletes();
        });
    }
0 likes
6 replies
LaryAI's avatar
Level 58

The error message indicates that the "start_date" column in the "bookings" table is set to NOT NULL, but the insert statement is not providing a value for it. To solve this error, you need to make sure that a value for "start_date" is provided when inserting a new record into the "bookings" table.

For example, you can modify the insert statement to include a value for "start_date":

DB::table('bookings')->insert([
    'apartment_id' => 1,
    'user_id' => 2,
    'start_date' => '2023-05-29', // add a value for start_date
    'end_date' => '2023-06-05',
    'guests_adults' => 2,
    'guests_children' => 1,
    'total_price' => 1000,
    'created_at' => now(),
    'updated_at' => now(),
]);

Alternatively, you can modify the migration file to provide a default value for "start_date" so that it is not required:

$table->date('start_date')->nullable()->default(null);
dacfabre's avatar

@ngozistephen

            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->unsignedInteger('guests_adults')->nullable(); or  $table->unsignedInteger('guests_adults')->default(0);
            $table->unsignedInteger('guests_children')->nullable(); or  $table->unsignedInteger('guests_children')->default(0);
mzbk's avatar

Date and time are not integer values they should be in quotes either single or double. Both consider string.

$booking = new Booking();
$booking->apartment_id = 1;
$booking->user_id = 2;
$booking->start_date = '2023-05-29'; // Provide a valid start date
$booking->end_date = '2023-06-05'; // Set the end date as well
$booking->guests_adults = 2;
$booking->guests_children = 1;
$booking->total_price = 0;
$booking->save();

And always set nullable() as default for all columns when you create migrations.

$table->date('start_date')->nullable();
$table->date('end_date')->nullable();
$table->unsignedInteger('guests_adults')->nullable();
$table->unsignedInteger('guests_children')->nullable();
$table->unsignedInteger('total_price')->nullable();
Tray2's avatar

@mzbk The last bit advice is really bad, never ever set all columns as nullable. You should always be precise when building a database model, if the value required then it should never be nullable, the database is always the last sentinel that protects you, and also consider a new developer to the project, and their confusion when something is allowed to be null in the database but not in the backend.

1 like
Tray2's avatar

@ngozistephen When setting created_at and updated_at, you really should use now() to set the timestamp.

Please or to participate in this conversation.