Laravel 5.6:表创建失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48744687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6: Table creation failed
提问by Brigo
I'm new to Laravel and I'm trying to create tables using Schema
fa?ade. I create the migration file with command
我是 Laravel 的新手,我正在尝试使用Schema
fa?ade创建表格。我用命令创建迁移文件
php artisan make:migration create_products_define_standards_table --create=products_define_standards
Here's the file:
这是文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStandardsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products_define_standards', function (Blueprint $table) {
$table->increments('id');
$table->string('code')->unique();
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products_define_standards');
}
}
It really has almost the same content of the default CreateUsersTable
, but when I run php artisan migrate
it creates:
它确实具有与 default 几乎相同的内容CreateUsersTable
,但是当我运行php artisan migrate
它时会创建:
users
' table (default)migrations
' table (default)
users
' 表(默认)migrations
' 表(默认)
but not:
但不是:
password_resets
' table (default)products_define_standards
' table (custom)
password_resets
' 表(默认)products_define_standards
' 表(自定义)
I tried with php artisan migrate:fresh
but I get the same log:
我试过,php artisan migrate:fresh
但我得到相同的日志:
Dropped all tables successfully.
Migration table created successfully.
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
at /home/whatever/whatever.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php: 664
660: // If an exception occurs when attempting to run a query, we'll format the error
661: // message to include the bindings with SQL, which will make this exception a
662: // lot more helpful to the developer instead of just the database's errors.
663: catch (Exception $e) {
664: throw new QueryException(
665: $query, $this->prepareBindings($bindings), $e
666: );
667: }
668:
669: return $result;
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
/home/whatever/whatever.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php : 458
2 PDOStatement::execute()
/home/whatever/whatever.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php : 458
I found this answerand I run also composer dumpauto
but the result is the same.
我找到了这个答案,我也跑了,composer dumpauto
但结果是一样的。
Am I missing something? Should I do any other thing to register the migration somewhere else?
我错过了什么吗?我应该做任何其他事情来在其他地方注册迁移吗?
回答by Sapnesh Naik
Edit your AppServiceProvider.php
located in app/Providers
directory, and inside the boot()
method set a default string length.
编辑您AppServiceProvider.php
所在的app/Providers
目录,并在boot()
方法内部设置默认字符串长度。
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
回答by Farhad
In version 5.6 you should edit 2 files :
在 5.6 版中,您应该编辑 2 个文件:
First
第一的
Edit your AppServiceProvider.php located in app/Providers directory, and inside the boot() method set a default string length.
编辑位于 app/Providers 目录中的 AppServiceProvider.php,并在 boot() 方法中设置默认字符串长度。
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
Second
第二
Edit your database.php located in config/database.php directory
编辑位于 config/database.php 目录中的 database.php
in mysql configuration section 'engine'is null and you should replace with 'InnoDB ROW_FORMAT=DYNAMIC
'
在mysql配置部分'engine'为空,你应该用' InnoDB ROW_FORMAT=DYNAMIC
'替换
hope you enjoy.
希望你喜欢。
回答by Tiago Gouvêa
The approached that work here was pass a second param with the key name (a short one):
在这里工作的方法是传递带有键名的第二个参数(一个简短的):
$table->string('code')->unique(null,'unikcode');
回答by Psipherious
I would like to propose a potential better solution to this problem.
我想提出一个潜在的更好的解决方案来解决这个问题。
You don't have to edit any files that are part of your Laravel. Instead, edit your actual database collation and engine.
您不必编辑属于 Laravel 的任何文件。相反,编辑您的实际数据库排序规则和引擎。
I presume you're using either MySQL or MariaDB. Use phpMyAdmin, when you create your blank database, use utf8mb4_unicode_ci (utf8_unicode_ci or utf8P_general_ci may also work fine).
我假设您使用的是 MySQL 或 MariaDB。使用 phpMyAdmin,当您创建空白数据库时,请使用 utf8mb4_unicode_ci(utf8_unicode_ci 或 utf8P_general_ci 也可以正常工作)。
Next set your default database engine to InnoDB instead MyISAM (you can do this also in phpMyAdmin under the "Variables" tab, search for "engine".
接下来将您的默认数据库引擎设置为 InnoDB 而不是 MyISAM(您也可以在 phpMyAdmin 中的“变量”选项卡下执行此操作,搜索“引擎”。
The other solutions that people have proposed - i.e. editing database.php to make it use InnoDB anyways, have a very similar effect. But modern versions of MySQL/Maria shouldbe using InnoDB out of the box anyways.
人们提出的其他解决方案 - 即编辑 database.php 以使其无论如何都使用 InnoDB,具有非常相似的效果。但是无论如何,现代版本的 MySQL/Maria都应该开箱即用地使用 InnoDB。
Run your migration and it would execute fine with no further modifications.
运行您的迁移,无需进一步修改即可正常执行。