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

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

erore SQLSTATE[42000]:語法錯誤或訪問沖突:1075 表定義不正確;當我運行遷移

erore SQLSTATE[42000]:語法錯誤或訪問沖突:1075 表定義不正確;當我運行遷移

PHP
哆啦的時光機 2023-04-15 10:54:32
我在 laravel 中使用 otp 包(Laravel OTP 登錄包)我的遷移:public function up(){    Schema::create('one_time_password_logs', function (Blueprint $table) {        $table->increments('id');        $table->bigIncrements("user_id")->index();        $table->string('otp_code')->index();        $table->string('refer_number')->index();        $table->string('status')->index();        $table->timestamps();    });    Schema::table('one_time_password_logs', function (Blueprint $table) {        $table->foreign('user_id')            ->references('id')            ->on('users')            ->onDelete('cascade');    });}運行遷移時顯示此錯誤:Illuminate\Database\QueryException SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrecttable definition; there can be only one auto column and it must bedefined as a key (SQL: create table `one_time_password_logs` (`id` intunsi gned not null auto_increment primary key, `user_id` bigintunsigned not null auto_increment primary key, `otp_code` varchar(255)not null, `refer_number` varchar(255) not null, `status` varchar(255)not null, ` created_at` timestamp null, `updated_at` timestamp null)default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine =innodb)  atC:\Users\aliaz\Desktop\smart-lighting\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671    667|         // If an exception occurs when attempting to run a query, we'll format the error    668|         // message to include the bindings with SQL, which will make this exception a    669|         // lot more helpful to the developer instead of just the database's errors.    670|         catch (Exception $e) {  671|             throw new QueryException(    672|                 $query, $this->prepareBindings($bindings), $e    673|             );    674|         }    675| 
查看完整描述

1 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

  • 注意:Laravel 5.8 添加bigIncrements為默認值。

  • 帶遷移的外鍵:不要忘記UNSIGNED

  • InnoDB需要外鍵和引用鍵的索引,以便外鍵檢查可以快速進行并且不需要表掃描。

我合并了你的 2 Schema。嘗試這個 :

public function up()

{

    Schema::create('one_time_password_logs', function (Blueprint $table) {

        $table->engine = 'InnoDB';

        $table->bigIncrements('id');

        $table->bigInteger('user_id')->unsigned();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->string('otp_code')->index();

        $table->string('refer_number')->index();

        $table->string('status')->index();

        $table->timestamps();

    });

}


查看完整回答
反對 回復 2023-04-15
  • 1 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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