【问题标题】:sql error when seeding Specified key was too long for email播种时出现 sql 错误 指定的密钥对于电子邮件来说太长了
【发布时间】:2018-01-26 13:57:31
【问题描述】:

您好,我刚刚在 laravel 中启动了一个新项目,所以它是最新版本 5.4,在为单个用户播种到数据库时出现错误,并且出现此错误

[照亮\数据库\查询异常] SQLSTATE [42000]:语法错误或访问冲突:1071 指定的键是 t 太长了;最大密钥长度为 1000 字节(SQL:alter table users add unique users_email_unique(email))

[PDO异常] SQLSTATE [42000]:语法错误或访问冲突:1071 指定的键是 t 太长了;最大密钥长度为 1000 字节

User::create([
    'name' => 'someone'
    'email' => 'someone@outlook.com',
    'password' => bcrypt('mypassword'), 
]);


<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

【问题讨论】:

标签: php laravel


【解决方案1】:

你应该这样尝试:

use  Illuminate\Support\Facades\Schema; // At the top of your file

并在 AppServiceProvider 的 boot() 方法中添加这一行:

Schema::defaultStringLength(191);

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 2019-08-04
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多