gpt4 book ai didi

mysql - docker下迁移mysql5/laravel 5报错Specified key was too long

转载 作者:行者123 更新时间:2023-11-29 07:25:39 24 4
gpt4 key购买 nike

我尝试在 docker 下安装我的 laravel 5.7.19 应用程序并运行迁移我得到错误:

Migrating: 2018_01_01_145312_create_settings_table
Specified key was too long
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `settings` add unique `settings_name_unique`(`name`))

at /var/www/html/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|

Exception trace:

其中设置定义为:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name', 255)->unique();
$table->string('value', 255);
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable();

$table->index(['created_at'], 'settings_created_at_index');
});
Artisan::call('db:seed', array('--class' => 'SettingsWithInitData'));
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}

触发的播种器文件是 Votes/database/seeds/SettingsWithInitData.php,其中包含以下行:

public function run()
{
DB::table('settings')->insert([
'name' => 'site_name',
'value' => 'Select & Vote',
]);

DB::table('settings')->insert([
'name' => 'copyright_text',
'value' => '© 2018 - 2018 All rights reserved',
]);

DB::table('settings')->insert([
'name' => 'elastic_automation',
'value' => 'N',
]);

设置->名称字段都没有超过 40 个字符,我的灯系统没有任何问题。

在 phpmyadmin 中,我使用 utf8_general_ci 创建了新数据库。

检查在 phpmyadmin 中创建的表我没有在名称字段上看到唯一索引:https://imgur.com/a/2RfeyGnSHOW VARIABLES 有很大的输出,在这种情况下,哪些是有意义的?

怎么了?

我的 docker-compose.yml 有行:

version: '3.1'

services:

web:

build:
context: ./web
dockerfile: Dockerfile.yml

environment:
- APACHE_RUN_USER=www-data
volumes:
- ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
ports:
- 8081:80
working_dir: ${APP_PTH_CONTAINER}

db:
image: mysql:5.6.41
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1
volumes:
- ${DB_PATH_HOST}:/var/lib/mysql


phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8082:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: 1


composer:
image: composer:1.8
volumes:
- ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
working_dir: ${APP_PTH_CONTAINER}
command: composer install --ignore-platform-reqs

谢谢!

最佳答案

那是因为您在 v5.7.7 版本以下的 MySQL 上运行它。要解决您的问题,请更改 docker-compose.yml 上的 MySQL 版本,或者您可以编辑 AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
// add this line
Schema::defaultStringLength(191);
}

来源:https://laravel-news.com/laravel-5-4-key-too-long-error

关于mysql - docker下迁移mysql5/laravel 5报错Specified key was too long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53940399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com