Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
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
根据official Laravel 7.x documentation,你可以很容易地解决这个问题。
更新您的 /app/Providers/AppServiceProvider.php
以包含:
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
Alternatively, you may enable the innodb_large_prefix
option for your database. Refer to your database's documentation for instructions on how to properly enable this option.
此问题是由 Laravel 5.4 中的数据库版本引起的。
根据 docs(在 Index Lengths & MySQL / MariaDB
部分):
Laravel uses the utf8mb4
character set by default, which includes
support for storing "emojis" in the database. If you are running a
version of MySQL older than the 5.7.7 release or MariaDB older than
the 10.2.2 release, you may need to manually configure the default
string length generated by migrations in order for MySQL to create
indexes for them. You may configure this by calling the
Schema::defaultStringLength
method within your AppServiceProvider
.
换句话说,在<ROOT>/app/Providers/AppServiceProvider.php
:
// Import Schema
use Illuminate\Support\Facades\Schema;
// ...
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Add the following line
Schema::defaultStringLength(191);
}
// ...
}
但是正如对另一个答案的评论所说:
Be careful about this solution. If you index email fields for example,
stored emails can only have a max length of 191 chars. This is less
than the official RFC states.
所以文档也提出了另一种解决方案:
Alternatively, you may enable the innodb_large_prefix
option for your
database. Refer to your database's documentation for instructions on
how to properly enable this option.
对于不想改变的人AppServiceProvider.php
。
(在我看来,仅仅为了迁移而改变 AppServiceProvider.php
是个坏主意)
您可以将数据长度添加回database/migrations/
下的迁移文件,如下所示:
create_users_table.php
$table->string('name',64);
$table->string('email',128)->unique();
create_password_resets_table.php
$table->string('email',128)->index();
如果您想在 AppServiceProvider 中进行更改,则需要在迁移中定义电子邮件字段的长度。只需将第一行代码替换为第二行即可。
create_users_table
$table->string('email')->unique();
$table->string('email', 50)->unique();
create_password_resets_table
$table->string('email')->index();
$table->string('email', 50)->index();
修改成功后可以运行迁移。
注意:首先你必须删除(如果你有)users table, password_resets table 来自数据库并从迁移 table.
中删除用户和 password_resets 条目
与其设置长度限制,不如我提出以下建议,这对我有用。
内部:
config/database.php
将此行替换为 mysql:
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
与:
'engine' => null,
不知道为什么会出现上面的解决方案和官方添加的解决方案
Schema::defaultStringLength(191);
在 AppServiceProvider
中对我不起作用。
有用的是编辑 config
文件夹中的 database.php
文件。
只需编辑
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
至
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
它应该可以工作,虽然您将无法存储扩展的多字节字符,例如表情符号。
这是一个丑陋的 hack,如果你想用非英语语言存储字符串,请不要这样做,emoji
我用 Laravel 5.7 做到了。希望对你有帮助。
不要忘记停止并再次启动服务器。
我只是在这里添加这个答案,因为它是我的 quickest
解决方案。只需将默认数据库引擎设置为 'InnoDB'
on
/config/database.php
'mysql' => [
...,
...,
'engine' => 'InnoDB',
]
然后运行php artisan config:cache
清除并刷新配置缓存
编辑:
找到的答案 here 可能会解释这个
的幕后故事
为了避免更改代码中的任何内容,只需将您的MySQL服务器至少更新到 5.7.7
参考此以获取更多信息:https://laravel-news.com/laravel-5-4-key-too-long-error
如前所述,我们将 AppServiceProvider.php 添加到 App/Providers
use Illuminate\Support\Facades\Schema; // add this
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191); // also this line
}
您可以在下面的 link 中查看更多详细信息(搜索 "Index Lengths & MySQL / MariaDB")
https://laravel.com/docs/5.5/migrations
但是,这不是我发布的全部内容!即使在执行上述操作时,问题仍然存在你可能会遇到另一个错误 (那时候你运行 php artisan migrate
命令,因为长度的问题,操作很可能卡在中间。解决方法如下,用户table 可能是在没有其余部分的情况下创建的,或者不完全正确)
我们需要回滚。默认回滚将不起作用。因为迁移操作不喜欢完成。您需要手动删除数据库中新创建的 tables。
我们可以使用 tinker 来完成,如下所示:
L:\todos> php artisan tinker
Psy Shell v0.8.15 (PHP 7.1.10 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
我自己对用户有意见table。
之后就可以开始了
php artisan migrate:rollback
php artisan migrate
如迁移 guide 中所述,要解决此问题,您只需编辑 app/Providers/AppServiceProvider.php
文件并在启动方法中设置默认字符串长度:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
注意:首先您必须删除(如果有)用户 table, password_resets table 从数据库中删除用户和 password_resets 条目 migrations table.
要运行所有未完成的迁移,请执行migrate
Artisan 命令:
php artisan migrate
之后一切都应该正常工作。
我找到了两个解决这个错误的方法
选项 1:
在database/migrations中打开您的用户和password_resettable 文件夹
只需更改电子邮件的长度:
$table->string('email',191)->unique();
选项 2:
打开你的 app/Providers/AppServiceProvider.php
文件并在 boot()
方法设置默认字符串长度:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
这很常见,因为 Laravel 5.4 将默认数据库字符集更改为 utf8mb4。您需要做的是:通过将此代码放在 class 声明
之前来编辑 App\Providers.php
use Illuminate\Support\Facades\Schema;
此外,将此添加到 'boot' 函数
Schema::defaultStringLength(191);
我已经解决了这个问题并编辑了我的 config->database.php
文件以喜欢我的数据库 ('charset'=>'utf8')
和('collation'=>'utf8_general_ci')
,所以我的问题解决了代码如下:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Schema::defaultStringLength(191);
将默认定义所有字符串的长度 191,这可能会破坏您的数据库。你一定不能走这条路。
只需定义数据库迁移中任何特定列的长度class。例如,我在 CreateUsersTable
class 中定义 "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();
});
}
对于可能 运行 涉及此问题的任何其他人,我的问题是我正在创建一个类型为 string
的列,并在我打算将其设置为 ->unsigned()
时尝试将其设置为 ->unsigned()
是一个整数。
我认为强制 StringLenght 为 191 是一个非常糟糕的主意。
所以我调查了解发生了什么。
我注意到这条消息错误:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key
was too long; max key length is 767 bytes
在我更新 MySQL 版本后开始出现。所以我用 PHPMyAdmin 检查了表格,我注意到所有创建的新表格都使用排序规则 utf8mb4_unicode_ci 而不是 utf8_unicode_ci旧的
在我的学说配置文件中,我注意到字符集设置为 utf8mb4,但我以前的所有表都是在 utf8 中创建的,所以我想这是它开始在 utf8mb4 上工作的一些更新魔法。
现在最简单的解决方法是更改 ORM 配置文件中的行字符集。
如果您处于开发模式,则使用 utf8mb4_unicode_ci 删除表,或者如果您不能删除它们,请修复字符集。
对于 Symfony 4
change charset: utf8mb4 to charset: utf8 in config/packages/doctrine.yaml
现在我的学说迁移又恢复正常了。
我正在添加两个对我有用的sollution。
- 打开 database.php 文件 config dir/folder.
将 'engine' => null,
编辑为 'engine' => 'InnoDB',
这对我有用。
第二个解决方案是:
打开 database.php 文件插入 config dir/folder.
2.编辑
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
至
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
祝你好运
这里工作的方法是传递带有键名(一个短的)的第二个参数:
$table->string('my_field_name')->unique(null,'key_name');
我刚刚修改了 users
和 password_resets
迁移文件中的以下行。
旧:$table->string('email')->unique();
新:$table->string('email', 128)->unique();
推荐的解决方案是启用 MySQL 的 innodb_large_prefix
选项,这样您就不会遇到后续问题。以下是如何做到这一点:
打开 my.ini
MySQL 配置文件并在 [mysqld]
行下添加以下行,如下所示。
[mysqld]
innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_file_per_table = ON
之后,保存您的更改并重新启动您的 MySQL 服务。
如果需要回滚,然后重新运行 迁移。
以防万一您的问题仍然存在,请转到您的数据库配置文件并设置
'engine' => null,
到 'engine' => 'innodb row_format=dynamic'
希望对您有所帮助!
1- 转到 /config/database.php
并找到这些行
'mysql' => [
...,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
...,
'engine' => null,
]
并将它们更改为:
'mysql' => [
...,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
...,
'engine' => 'InnoDB',
]
2- 运行 php artisan config:cache
重新配置 laravel
3- 删除数据库中现有的表,然后 运行 php artisan migrate
再次
如果您还没有为数据库分配任何数据,请执行以下操作:
- 转到 app/Providers/AppServiceProvide.php 并添加
use Illuminate\Support\ServiceProvider;
以及方法 boot() 的内部;
Schema::defaultStringLength(191);
现在删除数据库中的记录,用户 table for ex.
运行以下
php artisan config:cache
php artisan migrate
即使我已经遇到了这个错误(实际上因为我已经遇到了)
Schema::defaultStringLength(191);在我的 AppServiceProvider.php 文件中。
原因是因为我试图在我的一个迁移中将字符串值设置为高于 191 的值:
Schema::create('order_items', function (Blueprint $table) {
$table->primary(['order_id', 'product_id', 'attributes']);
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->string('attributes', 1000); // This line right here
$table->timestamps();
});
删除 1000 或将其设置为 191 解决了我的问题。
将我的本地数据库服务器类型从 "mariadb" 更改为 "mysql" 为我解决了这个问题,而无需编辑任何 Laravel 个文件。
我按照本教程更改了我的数据库服务器类型:https://odan.github.io/2017/08/13/xampp-replacing-mariadb-with-mysql.html
您可以按如下方式设置索引字段的字符串长度:
$table->string('email', 200)->unique();
对于流明:
添加到 .env
文件
DB_ENGINE=InnoDB
首先删除localhost中数据库的所有表
将文件 config/database.php 中的 Laravel 默认数据库 (utf8mb4) 属性更改为:
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
之后
更改我的本地数据库属性 utf8_unicode_ci。
php artisan 迁移
没关系。
没有人告诉的解决方案是在Mysqlv5.5及以后的InnoDB是默认的存储引擎没有这个问题,但在很多情况下,像我这样有一些旧的 mysql ini 配置文件使用旧的 MYISAM 存储引擎,如下所示。
default-storage-engine=MYISAM
这造成了所有这些问题,解决方案是在 Mysql 的 ini[ 中将 default-storage-engine 更改为 InnoDB =31=] 一劳永逸的配置文件,而不是临时修改。
default-storage-engine=InnoDB
如果您使用的是 MySql v5.5 或更高版本,那么 InnoDB 是默认引擎,因此您不需要需要像上面那样明确地设置它,只需删除 default-storage-engine=MYISAM
如果它存在于你的 ini
文件中,你就可以开始了。
只需在 /etc/mysql/mariadb.conf.d/50-server.cnf 或您的配置所在的任何位置添加几行:
innodb-file-format=barracuda
innodb-file-per-table=ON
innodb-large-prefix=ON
innodb_default_row_format = 'DYNAMIC'
和sudo service mysql restart
查看文档:https://dev.mysql.com/doc/refman/5.6/en/innodb-row-format.html
需要使用以下命令创建数据库。
CREATE DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;
然后 运行 按照命令迁移数据库表。
php artisan migrate
如果您遇到此错误 运行ning“php artisan migrate”。您可以通过编写以下代码来更改要更新的 table:
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
再
Laravel 7.X(也适用于 8X): 简单 解决方案.
选项-1:
php artisan db:wipe
在/config/database.php中更新mysql数组的这些值(以下)
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
然后
php artisan migrate
完成! 将创建迁移表成功。
选项-2:
手动使用php artisan db:wipe
或delete/drop您数据库的所有表。
更新你的AppServiceProvider.php[位于 app/Providers/AppServiceProvider.php ]
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
然后
php artisan migrate
大功告成!
陷阱:我想提一下@shock_gone_wild的评论
Be careful about this solution (Option-2). If you index email fields for example,
stored emails can only have a max length of 191 chars. This is less
than the official RFC states.
可选我尝试了这些可能的方法(如下所示)但不起作用。
php artisan config:cache
php artisan migrate:fresh
php artisan migrate:reset
对我来说就像魅力一样!
将此添加到 config/database。php
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
而不是
'engine' => 'null',
尝试使用默认字符串长度 125(对于 MySQL 8.0)。
defaultStringLength(125)
最干净的解决方案是转到您的数据库并更改:
default_storage_engine to InnoDB
你很可能有 MyISAM。
在此处打开此文件:/app/Providers/AppServiceProvider.php
并将此代码更新为我的图片:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
在database.php
-添加这一行:
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
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 uniqueusers_email_unique
([PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
根据official Laravel 7.x documentation,你可以很容易地解决这个问题。
更新您的 /app/Providers/AppServiceProvider.php
以包含:
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
Alternatively, you may enable the
innodb_large_prefix
option for your database. Refer to your database's documentation for instructions on how to properly enable this option.
此问题是由 Laravel 5.4 中的数据库版本引起的。
根据 docs(在 Index Lengths & MySQL / MariaDB
部分):
Laravel uses the
utf8mb4
character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them. You may configure this by calling theSchema::defaultStringLength
method within yourAppServiceProvider
.
换句话说,在<ROOT>/app/Providers/AppServiceProvider.php
:
// Import Schema
use Illuminate\Support\Facades\Schema;
// ...
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Add the following line
Schema::defaultStringLength(191);
}
// ...
}
但是正如对另一个答案的评论所说:
Be careful about this solution. If you index email fields for example, stored emails can only have a max length of 191 chars. This is less than the official RFC states.
所以文档也提出了另一种解决方案:
Alternatively, you may enable the
innodb_large_prefix
option for your database. Refer to your database's documentation for instructions on how to properly enable this option.
对于不想改变的人AppServiceProvider.php
。
(在我看来,仅仅为了迁移而改变 AppServiceProvider.php
是个坏主意)
您可以将数据长度添加回database/migrations/
下的迁移文件,如下所示:
create_users_table.php
$table->string('name',64);
$table->string('email',128)->unique();
create_password_resets_table.php
$table->string('email',128)->index();
如果您想在 AppServiceProvider 中进行更改,则需要在迁移中定义电子邮件字段的长度。只需将第一行代码替换为第二行即可。
create_users_table
$table->string('email')->unique();
$table->string('email', 50)->unique();
create_password_resets_table
$table->string('email')->index();
$table->string('email', 50)->index();
修改成功后可以运行迁移。
注意:首先你必须删除(如果你有)users table, password_resets table 来自数据库并从迁移 table.
与其设置长度限制,不如我提出以下建议,这对我有用。
内部:
config/database.php
将此行替换为 mysql:
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
与:
'engine' => null,
不知道为什么会出现上面的解决方案和官方添加的解决方案
Schema::defaultStringLength(191);
在 AppServiceProvider
中对我不起作用。
有用的是编辑 config
文件夹中的 database.php
文件。
只需编辑
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
至
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
它应该可以工作,虽然您将无法存储扩展的多字节字符,例如表情符号。
这是一个丑陋的 hack,如果你想用非英语语言存储字符串,请不要这样做,emoji
我用 Laravel 5.7 做到了。希望对你有帮助。
不要忘记停止并再次启动服务器。
我只是在这里添加这个答案,因为它是我的 quickest
解决方案。只需将默认数据库引擎设置为 'InnoDB'
on
/config/database.php
'mysql' => [
...,
...,
'engine' => 'InnoDB',
]
然后运行php artisan config:cache
清除并刷新配置缓存
编辑: 找到的答案 here 可能会解释这个
的幕后故事为了避免更改代码中的任何内容,只需将您的MySQL服务器至少更新到 5.7.7
参考此以获取更多信息:https://laravel-news.com/laravel-5-4-key-too-long-error
如前所述,我们将 AppServiceProvider.php 添加到 App/Providers
use Illuminate\Support\Facades\Schema; // add this
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191); // also this line
}
您可以在下面的 link 中查看更多详细信息(搜索 "Index Lengths & MySQL / MariaDB") https://laravel.com/docs/5.5/migrations
但是,这不是我发布的全部内容!即使在执行上述操作时,问题仍然存在你可能会遇到另一个错误 (那时候你运行 php artisan migrate
命令,因为长度的问题,操作很可能卡在中间。解决方法如下,用户table 可能是在没有其余部分的情况下创建的,或者不完全正确)
我们需要回滚。默认回滚将不起作用。因为迁移操作不喜欢完成。您需要手动删除数据库中新创建的 tables。
我们可以使用 tinker 来完成,如下所示:
L:\todos> php artisan tinker
Psy Shell v0.8.15 (PHP 7.1.10 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
我自己对用户有意见table。
之后就可以开始了
php artisan migrate:rollback
php artisan migrate
如迁移 guide 中所述,要解决此问题,您只需编辑 app/Providers/AppServiceProvider.php
文件并在启动方法中设置默认字符串长度:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
注意:首先您必须删除(如果有)用户 table, password_resets table 从数据库中删除用户和 password_resets 条目 migrations table.
要运行所有未完成的迁移,请执行migrate
Artisan 命令:
php artisan migrate
之后一切都应该正常工作。
我找到了两个解决这个错误的方法
选项 1:
在database/migrations中打开您的用户和password_resettable 文件夹
只需更改电子邮件的长度:
$table->string('email',191)->unique();
选项 2:
打开你的 app/Providers/AppServiceProvider.php
文件并在 boot()
方法设置默认字符串长度:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
这很常见,因为 Laravel 5.4 将默认数据库字符集更改为 utf8mb4。您需要做的是:通过将此代码放在 class 声明
之前来编辑 App\Providers.phpuse Illuminate\Support\Facades\Schema;
此外,将此添加到 'boot' 函数
Schema::defaultStringLength(191);
我已经解决了这个问题并编辑了我的 config->database.php
文件以喜欢我的数据库 ('charset'=>'utf8')
和('collation'=>'utf8_general_ci')
,所以我的问题解决了代码如下:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Schema::defaultStringLength(191);
将默认定义所有字符串的长度 191,这可能会破坏您的数据库。你一定不能走这条路。
只需定义数据库迁移中任何特定列的长度class。例如,我在 CreateUsersTable
class 中定义 "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();
});
}
对于可能 运行 涉及此问题的任何其他人,我的问题是我正在创建一个类型为 string
的列,并在我打算将其设置为 ->unsigned()
时尝试将其设置为 ->unsigned()
是一个整数。
我认为强制 StringLenght 为 191 是一个非常糟糕的主意。 所以我调查了解发生了什么。
我注意到这条消息错误:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
在我更新 MySQL 版本后开始出现。所以我用 PHPMyAdmin 检查了表格,我注意到所有创建的新表格都使用排序规则 utf8mb4_unicode_ci 而不是 utf8_unicode_ci旧的
在我的学说配置文件中,我注意到字符集设置为 utf8mb4,但我以前的所有表都是在 utf8 中创建的,所以我想这是它开始在 utf8mb4 上工作的一些更新魔法。
现在最简单的解决方法是更改 ORM 配置文件中的行字符集。 如果您处于开发模式,则使用 utf8mb4_unicode_ci 删除表,或者如果您不能删除它们,请修复字符集。
对于 Symfony 4
change charset: utf8mb4 to charset: utf8 in config/packages/doctrine.yaml
现在我的学说迁移又恢复正常了。
我正在添加两个对我有用的sollution。
- 打开 database.php 文件 config dir/folder.
将
'engine' => null,
编辑为'engine' => 'InnoDB',
这对我有用。
第二个解决方案是:
打开 database.php 文件插入 config dir/folder.
2.编辑
'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci',
至'charset' => 'utf8', 'collation' => 'utf8_unicode_ci',
祝你好运
这里工作的方法是传递带有键名(一个短的)的第二个参数:
$table->string('my_field_name')->unique(null,'key_name');
我刚刚修改了 users
和 password_resets
迁移文件中的以下行。
旧:$table->string('email')->unique();
新:$table->string('email', 128)->unique();
推荐的解决方案是启用 MySQL 的 innodb_large_prefix
选项,这样您就不会遇到后续问题。以下是如何做到这一点:
打开 my.ini
MySQL 配置文件并在 [mysqld]
行下添加以下行,如下所示。
[mysqld]
innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_file_per_table = ON
之后,保存您的更改并重新启动您的 MySQL 服务。
如果需要回滚,然后重新运行 迁移。
以防万一您的问题仍然存在,请转到您的数据库配置文件并设置
'engine' => null,
到 'engine' => 'innodb row_format=dynamic'
希望对您有所帮助!
1- 转到 /config/database.php
并找到这些行
'mysql' => [
...,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
...,
'engine' => null,
]
并将它们更改为:
'mysql' => [
...,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
...,
'engine' => 'InnoDB',
]
2- 运行 php artisan config:cache
重新配置 laravel
3- 删除数据库中现有的表,然后 运行 php artisan migrate
再次
如果您还没有为数据库分配任何数据,请执行以下操作:
- 转到 app/Providers/AppServiceProvide.php 并添加
use Illuminate\Support\ServiceProvider;
以及方法 boot() 的内部;
Schema::defaultStringLength(191);
现在删除数据库中的记录,用户 table for ex.
运行以下
php artisan config:cache
php artisan migrate
即使我已经遇到了这个错误(实际上因为我已经遇到了) Schema::defaultStringLength(191);在我的 AppServiceProvider.php 文件中。
原因是因为我试图在我的一个迁移中将字符串值设置为高于 191 的值:
Schema::create('order_items', function (Blueprint $table) {
$table->primary(['order_id', 'product_id', 'attributes']);
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->string('attributes', 1000); // This line right here
$table->timestamps();
});
删除 1000 或将其设置为 191 解决了我的问题。
将我的本地数据库服务器类型从 "mariadb" 更改为 "mysql" 为我解决了这个问题,而无需编辑任何 Laravel 个文件。
我按照本教程更改了我的数据库服务器类型:https://odan.github.io/2017/08/13/xampp-replacing-mariadb-with-mysql.html
您可以按如下方式设置索引字段的字符串长度:
$table->string('email', 200)->unique();
对于流明:
添加到 .env
文件
DB_ENGINE=InnoDB
首先删除localhost中数据库的所有表
将文件 config/database.php 中的 Laravel 默认数据库 (utf8mb4) 属性更改为:
'charset' => 'utf8', 'collation' => 'utf8_unicode_ci',
之后 更改我的本地数据库属性 utf8_unicode_ci。 php artisan 迁移 没关系。
没有人告诉的解决方案是在Mysqlv5.5及以后的InnoDB是默认的存储引擎没有这个问题,但在很多情况下,像我这样有一些旧的 mysql ini 配置文件使用旧的 MYISAM 存储引擎,如下所示。
default-storage-engine=MYISAM
这造成了所有这些问题,解决方案是在 Mysql 的 ini[ 中将 default-storage-engine 更改为 InnoDB =31=] 一劳永逸的配置文件,而不是临时修改。
default-storage-engine=InnoDB
如果您使用的是 MySql v5.5 或更高版本,那么 InnoDB 是默认引擎,因此您不需要需要像上面那样明确地设置它,只需删除 default-storage-engine=MYISAM
如果它存在于你的 ini
文件中,你就可以开始了。
只需在 /etc/mysql/mariadb.conf.d/50-server.cnf 或您的配置所在的任何位置添加几行:
innodb-file-format=barracuda
innodb-file-per-table=ON
innodb-large-prefix=ON
innodb_default_row_format = 'DYNAMIC'
和sudo service mysql restart
查看文档:https://dev.mysql.com/doc/refman/5.6/en/innodb-row-format.html
需要使用以下命令创建数据库。
CREATE DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;
然后 运行 按照命令迁移数据库表。
php artisan migrate
如果您遇到此错误 运行ning“php artisan migrate”。您可以通过编写以下代码来更改要更新的 table:
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
再
Laravel 7.X(也适用于 8X): 简单 解决方案.
选项-1:
php artisan db:wipe
在/config/database.php中更新mysql数组的这些值(以下)
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
然后
php artisan migrate
完成! 将创建迁移表成功。
选项-2:
手动使用php artisan db:wipe
或delete/drop您数据库的所有表。
更新你的AppServiceProvider.php[位于 app/Providers/AppServiceProvider.php ]
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
然后
php artisan migrate
大功告成!
陷阱:我想提一下@shock_gone_wild的评论
Be careful about this solution (Option-2). If you index email fields for example, stored emails can only have a max length of 191 chars. This is less than the official RFC states.
可选我尝试了这些可能的方法(如下所示)但不起作用。
php artisan config:cache
php artisan migrate:fresh
php artisan migrate:reset
对我来说就像魅力一样!
将此添加到 config/database。php
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
而不是
'engine' => 'null',
尝试使用默认字符串长度 125(对于 MySQL 8.0)。
defaultStringLength(125)
最干净的解决方案是转到您的数据库并更改:
default_storage_engine to InnoDB
你很可能有 MyISAM。
在此处打开此文件:/app/Providers/AppServiceProvider.php
并将此代码更新为我的图片:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
在database.php
-添加这一行:
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',