abdzohbi
1 month ago
147
4
Laravel

Error in migration

Posted 1 month ago by abdzohbi

Hi Please Help Me this tables public function up() { Schema::disableForeignKeyConstraints();

    Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });
    
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title')->unique;
        $table->string('link')->nullable;
        $table->string('content')->nullable;
        $table->unsignedInteger('category_Id')->unsigned()->index();
        $table->string('imgfeatrued')->nullable;
        $table->string('tags')->nullable;
        $table->boolean('published')->nullable($value = 'true');
        $table->string('lang')->nullable ($value= 'all');
        $table->unsignedInteger('user')->unsigned()->index();
        $table->json('options')->nullable;  
        $table->ipAddress('visitor')->nullable; 
        $table->foreign('category_Id')-> references('id')-> on('categories')-> onDelete('cascade');
        $table->foreign('user')-> references('id')->on('users')-> onDelete('cascade');
        $table->timestamps();
        $table->timestamp('published_at')->nullable;


    });

    Schema::create('posts_content', function (Blueprint $table){
        $table->bigIncrements('id');
        $table->bigInteger('postid');
        $table->text('content');
        $table->forgein('postid')-> references('id')-> on('posts')->onDelete('cascade');
        $table->timestamps();


    });
}

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

}

whene migration giv me :

lluminate\Database\QueryException : SQLSTATE[HY000]: General error: 1825 Failed to add the foreign key constraint on table 'posts'. Incorrect options in FOREIGN KEY constraint 'varweb/posts_category_id_foreign' (SQL: alter table posts add constraint posts_category_id_foreign foreign key (category_Id) references categories (id) on delete cascade)

at /mnt/Data/www/varweb/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669 665| // If an exception occurs when attempting to run a query, we'll format the error 666| // message to include the bindings with SQL, which will make this exception a 667| // lot more helpful to the developer instead of just the database's errors. 668| catch (Exception $e) {

669| throw new QueryException( 670| $query, $this->prepareBindings($bindings), $e 671| ); 672| } 673|

Exception trace:

1 PDOException::("SQLSTATE[HY000]: General error: 1825 Failed to add the foreign key constraint on table 'posts'. Incorrect options in FOREIGN KEY constraint 'varweb/posts_category_id_foreign'") /mnt/Data/www/varweb/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

2 PDOStatement::execute() /mnt/Data/www/varweb/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

Please use the argument -v to see more details.

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