我正在嘗試在 Laravel 中創(chuàng)建外鍵,但是當(dāng)我使用 artisan 遷移表時(shí),出現(xiàn)以下錯(cuò)誤: 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'); }}這是我的產(chǎn)品遷移<?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中將數(shù)據(jù)庫引擎更改為Inno DB,但它根本沒有幫助。
1 回答

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