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

nizam0786's avatar

Integrity constraint violation after installing local web app live on forge digital ocean server.

I keep getting the following two errors below, when trying to delete a bus model and its only happening on the live version(forge, digital ocean) of a web app which I have built.

Bus has one manufacturer and manufacturer model has many buses, I want to be able to delete a single bus without deleting the manufacturer model and also without removing other buses associated with the manufacturer so cascade on delete doesn't really help as it deletes all the buses connected to the related manufacturer.

Note: It all works offline locally using phpmyadmin, wamp thats what is confusing me, Its my first website that I have put up live so please need some help.

Thanks in advance.

Manufacturer Class


 public function buses()
{
        return $this->hasMany('App\Bus');
}

Bus Class


 public function manufacturer()
{
        return $this->belongsTo('App\Manufacturer');
    }

Buses Table Migration


Schema::create('buses', function (Blueprint $table) 
{           
        $table->increments('id');
            $table->string('title');
            $table->integer('manufacturer_id')->unsigned()->default(1);
            $table->foreign('manufacturer_id')->references('id')->on('buses');
            $table->string('model');
            $table->string('registration' , 15);
            $table->string('body');
            $table->string('chasis');
            $table->integer('seating');
            $table->integer('standing');
            $table->date('reg_date');
            $table->date('expiry_date');
            $table->string('dda');
            $table->text('description');
            $table->integer('price');
            $table->integer('sold')->default(0);
            $table->timestamps();
        });

Manufacturers Table Migration


Schema::create('manufacturers', function (Blueprint $table) 
    {
            $table->increments('id');
            $table->string('name')->unique();
            $table->timestamps();
        });

Two Exceptions that keep coming up when attempting to delete bus model

QueryException in Connection.php line 770: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (sherwood_cars.buses, CONSTRAINT buses_manufacturer_id_foreign FOREIGN KEY (manufacturer_id) REFERENCES buses (id)) (SQL: delete from buses where id = 1)

PDOException in Connection.php line 505: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (sherwood_cars.buses, CONSTRAINT buses_manufacturer_id_foreign FOREIGN KEY (manufacturer_id) REFERENCES buses (id))

0 likes
5 replies
nizam0786's avatar

I think I have figured out why it is doing.... the database I am using locally is using storage engine MyISAM but the live version is Innodb.

As soon as I changed the local database to Innodb the same exceptions started to appear.

now i need to figure out how I can turn off the foreign key checks...?

Cronix's avatar

DB::statement('SET FOREIGN_KEY_CHECKS = 0');

1 like
Corys8646's avatar

$table->foreign('manufacturer_id')->references('id')->on('buses');

Should that be on('manufacturers') ?

1 like
nizam0786's avatar

@Corys8646 yes you right need to change that thanks for pointing it out.

@Cronix where should i put the db statment should it be im the migration for the buses table?

kind regards,

Nizam

nizam0786's avatar
nizam0786
OP
Best Answer
Level 10

It was the ordering of my migration files.

I had to change the timestamp of the bus table and make sure it is migrated after the manufacturer table.

now everything is working as normal.

Please or to participate in this conversation.