jeudi 3 septembre 2020

Laravel 5: errno: 150 "Foreign key constraint is incorrectly formed

I'm trying to add a foreign key constraint to the notifications table migration (Laravel 5.8) I've tried this a few different ways. I separated them as suggested on other posts, but I haven't been able to find the solution to my problem.

PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `appealmaker`.`notifications` (errno: 150 "Foreign key constraint is incorrectly formed")")

The migration looks like this:

class CreateNotificationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('type');
            $table->morphs('notifiable');
            $table->text('data');
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });

        Schema::table('notifications', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('notifications');
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire