Laravel 8 關(guān)于 MySQL 和 MariaDB 的索引長(zhǎng)度

2021-07-07 10:53 更新

Laravel 默認(rèn)使用 utf8mb4 編碼,它支持在數(shù)據(jù)庫(kù)中儲(chǔ)存 emojis 。如果你是在版本低于 5.7.7 的 MySQL 或者版本低于 10.2.2 的 MariaDB 上創(chuàng)建索引,那你就需要手動(dòng)配置數(shù)據(jù)庫(kù)遷移的默認(rèn)字符串長(zhǎng)度。即在 AppServiceProvider 中調(diào)用 Schema::defaultStringLength 方法來(lái)配置它:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
     Schema::defaultStringLength(191);
} 

當(dāng)然,你也可以選擇開(kāi)啟數(shù)據(jù)庫(kù)的 innodb_large_prefix 選項(xiàng)。至于如何正確開(kāi)啟,請(qǐng)自行查閱數(shù)據(jù)庫(kù)文檔。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)