[英]Laravel 5.7 Migration : key was too long
I want to make a multiple unique column, but when i run php artisan migrate
, i got this error:我想制作一个多个唯一的列,但是当我运行
php artisan migrate
时,我收到了这个错误:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;
SQLSTATE[42000]:语法错误或访问冲突:1071 指定的键太长; max key length is 1000 bytes
最大密钥长度为 1000 字节
This my code:这是我的代码:
Schema::create('buku', function (Blueprint $table) {
$table->increments('id');
$table->string('judul');
$table->string('pengarang');
$table->string('penerbit');
$table->string('thn_terbit',4);
$table->integer('stok');
$table->string('kategori');
$table->timestamps();
$table->unique(['judul','pengarang','penerbit','thn_terbit'],'unik');
});
this AppServiceProvider.php
file这个
AppServiceProvider.php
文件
public function boot()
{
Schema::defaultStringLength(191);
}
An answer will be appreciated一个答案将不胜感激
Version: Laravel 5.7版本:Laravel 5.7
Add the below code in app/Providers/AppServiceProvider.php
file在
app/Providers/AppServiceProvider.php
文件中添加以下代码
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
Note: The boot method is already there so you just need to add Schema::defaultStringLength(191);
注意: boot 方法已经存在,所以你只需要添加
Schema::defaultStringLength(191);
inside it, then delete old tables and re-migrate在里面,然后删除旧表并重新迁移
You are trying 4 columns for unique index.您正在为唯一索引尝试 4 列。 Sum of 4 columns can be up to 191 characters.
4 列的总和最多为 191 个字符。 Try each column with 47 characters.
尝试每列 47 个字符。
Schema::create('buku', function (Blueprint $table) {
$table->engine = 'innoDB';
$table->increments('id');
$table->string('judul', 47);
$table->string('pengarang', 47);
$table->string('penerbit', 47);
$table->string('thn_terbit', 47);
$table->integer('stok');
$table->string('kategori');
$table->timestamps();
$table->unique(['judul','pengarang','penerbit','thn_terbit'],'unik');
});
[Solved] [解决了]
I solved this by adding a $table->engine = 'innoDB';
我通过添加
$table->engine = 'innoDB';
解决了这个问题so the code look like this:所以代码看起来像这样:
Schema::create('buku', function (Blueprint $table) {
$table->engine = 'innoDB';
$table->increments('id');
$table->string('judul');
$table->string('pengarang');
$table->string('penerbit');
$table->string('thn_terbit');
$table->integer('stok');
$table->string('kategori');
$table->timestamps();
$table->unique(['judul','pengarang','penerbit','thn_terbit'],'unik');
});
and then re-migrate it, and everything is good.然后重新迁移它,一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.