Nosean
5 months ago

Foreign Key Constraints

Posted 5 months ago by Nosean

I become a error message when i try the migration:

QLSTATE[HY000]: General error: 1005 Can't create table blog.#sql-33c_b2 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table posts add constraint posts_user_id_foreign foreign key (user_id ) references users (id))

my Schema file:

        Schema::enableForeignKeyConstraints();

        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('body');
            $table->boolean('published')->default(false);
            $table->integer('user_id')->unsigned();
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users');
        });
``

Why ?

Please sign in or create an account to participate in this conversation.