我正在嘗試在 Laravel 中創建外鍵,但是當我使用 artisan 遷移表時,出現以下錯誤: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `products` add constraint `products_user_id_foreign` foreign key (`user_id`) references `users` (`id`))這是我的用戶遷移<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateUsersTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('surname')->nullable(); $table->string('showname')->nullable(); $table->string('business')->nullable(); $table->string('NIP')->nullable(); $table->string('PESEL')->nullable(); $table->string('address')->nullable(); $table->string('city')->nullable(); $table->string('postalcode')->nullable(); $table->string('phone')->nullable(); $table->string('comments')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); }}這是我的產品遷移<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateProductsTable extends Migration{ /** * Run the migrations. * * @return void */請幫我。我不知道該怎么辦。我在databaase.php中將數據庫引擎更改為Inno DB,但它根本沒有幫助。
1 回答

一只甜甜圈
TA貢獻1836條經驗 獲得超5個贊
Laravelincrements()
創建一個“自動遞增 UNSIGNED INTEGER(主鍵)等效列。?”。您的外鍵列user_id
必須與其引用的列具有完全相同的類型。
在您的CreateProductsTable
遷移中,更改
$table->integer('user_id')->nullable();
到
$table->integer('user_id')->unsigned()->nullable();
然后再試一次。
- 1 回答
- 0 關注
- 215 瀏覽
添加回答
舉報
0/150
提交
取消