簡體   English   中英

Laravel 5.7 遷移:密鑰太長

[英]Laravel 5.7 Migration : key was too long

我想制作一個多個唯一的列,但是當我運行php artisan migrate時,我收到了這個錯誤:

SQLSTATE[42000]:語法錯誤或訪問沖突:1071 指定的鍵太長; 最大密鑰長度為 1000 字節

這是我的代碼:

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');
    });

這個AppServiceProvider.php文件

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

一個答案將不勝感激

版本:Laravel 5.7

app/Providers/AppServiceProvider.php文件中添加以下代碼

use Illuminate\Support\Facades\Schema;

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

注意: boot 方法已經存在,所以你只需要添加Schema::defaultStringLength(191); 在里面,然后刪除舊表並重新遷移

您正在為唯一索引嘗試 4 列。 4 列的總和最多為 191 個字符。 嘗試每列 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');
});

[解決了]

我通過添加$table->engine = 'innoDB';解決了這個問題所以代碼看起來像這樣:

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');
    });

然后重新遷移它,一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM