在Laravel 5.4上使用php artisan make:auth迁移错误

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e users add unique users_email_unique(email)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes


当前回答

模式:defaultStringLength (191);将默认定义所有字符串191的长度,这可能会破坏您的数据库。你不能走这条路。

只需在数据库迁移类中定义任何特定列的长度。例如,我在CreateUsersTable类中定义了“name”,“username”和“email”,如下所示:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 191);
            $table->string('username', 30)->unique();
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

其他回答

如果您运行“php artisan migrate”时出现此错误。你可以这样修改你想要更新的表:

    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        

在迁移脚本中。例子:

class MyMigration extends Migration {

/**
 * Run the migrations.
 */
public function up()
{
    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        
    Schema::table('table_name', function ($table) {
        //....
    });
}

/**
 * Undo the migrations.
 */
public function down()
{
    //....
}
}

然后再次运行php artisan migrate

模式:defaultStringLength (191);将默认定义所有字符串191的长度,这可能会破坏您的数据库。你不能走这条路。

只需在数据库迁移类中定义任何特定列的长度。例如,我在CreateUsersTable类中定义了“name”,“username”和“email”,如下所示:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 191);
            $table->string('username', 30)->unique();
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

这里的方法是传递一个带有键名的第二个参数(一个短参数):

$table->string('my_field_name')->unique(null,'key_name');

将我的本地数据库服务器类型从“mariadb”更改为“mysql”,无需编辑任何Laravel文件就可以解决这个问题。

我按照本教程更改了我的db服务器类型:https://odan.github.io/2017/08/13/xampp-replacing-mariadb-with-mysql.html

对于这个错误,我找到了两个解决方案

选项1:

打开database/migrations文件夹下的user和password_reset表

只需要改变邮件的长度:

$table->string('email',191)->unique();

选项2:

打开app/Providers/AppServiceProvider.php文件,在boot()方法中设置一个默认字符串长度:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}