1
votes

I have some problems with migrating table on my laravel app. It always says

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

But I don't see a problem with foreign key, here is the migration

public function up()
    {
        Schema::create('replies', function (Blueprint $table) {
            $table->increments('id');
            $table->text('body');
            $table->integer('question_id')->unsigned();
            $table->integer('user_id');
            $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('replies');
    }

Btw, it's first adding this table and after that should add questions table, but error stops the migration. I set it to be first to migrate, but still same problem.

1

1 Answers

2
votes

This means that id on questions table is not an unsigned integer. question_id must be the same type in database as id on questions table. An other reason it may fail is because questions table and replies table don't have the same collation or the same engine.