你們能幫我嗎?我想在posts
表中新增一個在categories
表中有引用的外鍵。但是當我輸入命令php artisan migrate:fresh
時,它總是失敗。我得到的錯誤訊息是這樣的PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table 'handconsulting'.'posts' (errno: 150 "Foreignkey constraint is incorrectly formed")" )
這是我的posts
表格
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->foreignId('category_id')->constrained('categories')->onDelete('cascade')->onUpdate('cascade'); $table->string('slug')->unique(); $table->text('excerpt'); $table->text('body'); $table->string('iamge')->nullable(); $table->timestamp('published_at')->nullable(); $table->timestamps(); });
這是我的categories
表
Schema::create('categories', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->id(); $table->string('name')->unique(); $table->string('slug')->unique(); $table->timestamps(); });
this is my approach: