0

After adding $table->engine = 'MyISAM'; seem foreign key is not working anymore. This my main table

public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'MyISAM';
$table->integer('id');
$table->string('user_id',20);
$table->string('name');
$table->primary(['user_id','id']);
$table->rememberToken();
$table->timestamps();
});
DB::statement('ALTER TABLE users CHANGE id id INT(11) NOT NULL 
AUTO_INCREMENT');
}

Foreign key table

public function up()
{
    Schema::create('useraccesses', function (Blueprint $table) {
        $table->increments('id');
        $table->string('user_id',20);
        $table->datetime('accessTime')->nullable();
        $table->datetime('quittime')->nullable();
        $table->datetime('activeHour')->nullable();
        $table->text('token')->nullable();
        $table->foreign('user_id')
        ->references('user_id')->on('users')
        ->onDelete('cascade');
        $table->timestamps();
    });
}

Thanks

21bn
  • 117
  • 1
  • 2
  • 12

2 Answers2

0

Foreign keys are not supported for MyISAM tables in MySQL. Use InnoDB tables instead. c.f. https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html

anaxamaxan
  • 338
  • 2
  • 7
0

Try this

for users migration
...
$table->increments('id');
...

for useraccesses migration
...
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')
        ->references('user_id')->on('users')
        ->onDelete('cascade');
...