Laravel 5.6:表创建失败

时间:2018-02-12 10:51:37

标签: php laravel laravel-5 database-migration laravel-5.6

我是Laravel的新手,我正在尝试使用php artisan make:migration create_products_define_standards_table --create=products_define_standards façade创建表格。我使用命令

创建迁移文件
<?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');
    }
}

这是文件:

CreateUsersTable

它与默认php artisan migrate的内容几乎相同,但是当我运行users时,它会创建:

  • migrations'表(默认)
  • password_resets'表(默认)

  • products_define_standards'表(默认)
  • php artisan migrate:fresh'table(custom)

我尝试使用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 ,但我得到了相同的日志:

composer dumpauto

我找到this answer并且我也运行了public class BaseActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { private ActivityState homeState, contentState; @Override public void onActivityCreated(Activity activity, Bundle bundle) { if (activity instanceof HomeActivityv2) { homeState = ActivityState.CREATED; } else if (activity instanceof ContentDisplayActivity) { contentState = ActivityState.CREATED; } } @Override public void onActivityStarted(Activity activity) { if (activity instanceof HomeActivityv2) { homeState = ActivityState.STARTED; } else if (activity instanceof ContentDisplayActivity) { contentState = ActivityState.STARTED; } } @Override public void onActivityResumed(Activity activity) { if (activity instanceof HomeActivityv2) { homeState = ActivityState.RESUMED; } else if (activity instanceof ContentDisplayActivity) { contentState = ActivityState.RESUMED; } } @Override public void onActivityPaused(Activity activity) { if (activity instanceof HomeActivityv2) { homeState = ActivityState.PAUSED; } else if (activity instanceof ContentDisplayActivity) { contentState = ActivityState.PAUSED; } } @Override public void onActivityStopped(Activity activity) { if (activity instanceof HomeActivityv2) { homeState = ActivityState.STOPPED; } else if (activity instanceof ContentDisplayActivity) { contentState = ActivityState.STOPPED; } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { } @Override public void onActivityDestroyed(Activity activity) { if (activity instanceof HomeActivityv2) { homeState = ActivityState.DESTROYED; } else if (activity instanceof ContentDisplayActivity) { contentState = ActivityState.DESTROYED; } } public ActivityState getHomeState() { return homeState; } public ActivityState getContentState() { return contentState; } } ,但结果是一样的。

我错过了什么吗?我应该做其他任何事情来在其他地方注册迁移吗?

4 个答案:

答案 0 :(得分:9)

修改位于AppServiceProvider.php目录中的app/Providers,并在boot()方法内设置默认字符串长度。

use Illuminate\Support\Facades\Schema;

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

答案 1 :(得分:1)

在5.6版中,您应该编辑2个文件:

<强>第一

编辑位于app / Providers目录中的AppServiceProvider.php,并在boot()方法内设置默认字符串长度。

use Illuminate\Support\Facades\Schema;

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

<强>第二

编辑位于config / database.php目录中的database.php

在mysql配置部分&#39; engine&#39;为空,您应该替换为&#39; InnoDB ROW_FORMAT=DYNAMIC&#39;

希望你喜欢。

答案 2 :(得分:0)

在这里工作的方法是传递第二个参数,该参数的名称为键名(简称):

$table->string('code')->unique(null,'unikcode');

答案 3 :(得分:0)

我想为这个问题提出一个更好的解决方案。

您不必编辑Laravel中的任何文件。而是编辑实际的数据库排序规则和引擎。

我想您正在使用MySQL或MariaDB。使用phpMyAdmin,在创建空白数据库时,请使用utf8mb4_unicode_ci(utf8_unicode_ci或utf8P_general_ci也可以正常使用)。

接下来,将默认数据库引擎设置为InnoDB而不是MyISAM(也可以在phpMyAdmin中的“变量”选项卡下,搜索“引擎”。

人们提出的其他解决方案-即编辑database.php以使其无论如何都使用InnoDB,效果非常相似。但是,无论如何,现代版本的MySQL / Maria 应该立即使用InnoDB。

运行您的迁移,无需进一步修改即可正常执行。