亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

問題 SQLSTATE[HY000]:一般錯誤:1215 無法添加外鍵約束

問題 SQLSTATE[HY000]:一般錯誤:1215 無法添加外鍵約束

PHP
九州編程 2023-08-19 17:35:19
我正在嘗試在 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();

然后再試一次。


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 215 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號