【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'rbuckridge@example.com' for key 'users_email_unique'SQLSTATE [23000]:违反完整性约束:1062 键 'users_email_unique' 的重复条目 'rbuckridge@example.com'
【发布时间】:2021-03-19 17:25:48
【问题描述】:

这里是迁移代码:

    <?php
    
    use 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->id();
                $table->string('first_name');
                $table->string('last_name');
                $table->text('bio');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->string('profile_picture');
                $table->rememberToken();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('users');
        }
    }

【问题讨论】:

  • 你是在已经有数据的表上运行这个吗?错误很简单。
  • 迁移与该错误无关,您必须有一些迁移或播种器更改该表上的数据
  • 我猜你输入了两次电子邮件,并且电子邮件是唯一的。

标签: php laravel laravel-artisan


【解决方案1】:

迁移对您遇到的错误的影响较小。这是因为此时错误表明rbuckridge@example.com 已经存在于用户表中。这是通过-&gt;unique() 方法实现的,该方法可防止在同一表列中重新输入相同的数据。

我的建议是先把表里的数据全部清空再重新输入邮件或者换个不同的邮件。

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 2014-09-16
    • 2020-10-16
    • 2013-04-22
    • 2015-10-28
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多