Cannot add foreign key constraint on vps server

so I use my project locally and run this code php artisan migrateeverything works fine and my tables will be created

but when I go to my vps and do the same, I get this error

[Lighten \ Database \ QueryException] SQLSTATE [HY000]: general error: 1215 Unable to add foreign key constraint (SQL: alter table mediablesadd mediables_media_id_foreignforei gn constraint key ( media_id) refers to media( id) deletion of the cascade)

I followed some solution from this site, but they did not work

my migration file:

 Schema::create('media', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->nullable();
        $table->string('old_name')->nullable();
        $table->text('desc')->nullable();
        $table->string('category')->nullable();
        $table->string('type');
        $table->string('format');
        $table->string('href');
        $table->string('thumbnail')->nullable();

        $table->timestamps();
    });

    Schema::create('mediables', function (Blueprint $table) {

        $table->integer('media_id')->nullable();
        $table->integer('mediable_id');
        $table->string('mediable_type');
        $table->primary(['media_id','mediable_id','mediable_type']);

        $table->timestamps();
    });
    Schema::table('mediables', function (Blueprint $table) {
        $table->foreign('media_id')
            ->references('id')->on('media')
            ->onDelete('cascade');
    });

note that in my project there is a polymorphic relationship between the media and other models

vps , apache nginx vps,

+4
3

media id, media_id .

, id - increment(), unsigned():

$table->integer('media_id')->unsigned()->nullable();
+2

WAMP, mysql, ,

$table->foreign('media_id')
        ->references('id')->on('media')
        ->onDelete('cascade');

id media.

$table->primary(['media_id','mediable_id','mediable_type']); , id, $table->increments('id'); id .

+1

You need to set the table engine parameter InnoDBto default or set it explicitly with

$table->engine = 'InnoDB';

Only the mechanism InnoDBhandles foreign key constraints.

+1
source

Source: https://habr.com/ru/post/1664891/


All Articles