【问题标题】:Laravel migration doesn't add table in DBLaravel 迁移不会在数据库中添加表
【发布时间】:2018-07-18 20:47:11
【问题描述】:

我在文件2018_02_08_094356_create_currency_table.php..的迁移中添加了一个新表。

这是代码:

<?php

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

class CreateCurrencyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currency', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('usdeur', 15, 2);
            $table->decimal('usdchf', 15, 2);
            $table->timestamps();
        });
    }

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

当我运行php artisan migrate 时,我的数据库中只有默认的 laravel 用户(和迁移)表。没有货币。

可能是什么原因?

编辑

迁移一切正常。问题是这样的:

[Illuminate\Database\QueryException] SQLSTATE[42000]:语法错误或访问冲突:1071 指定的键太长;最大密钥长度 为 767 字节(SQL:alter table users add unique users_email_unique(email))

[PDOException] SQLSTATE[42000]:语法错误或访问冲突:1071 指定的密钥太长;最大密钥长度为 767 字节

解决方案是在AppServiceProvider.php中添加这个

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

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

【问题讨论】:

  • 如果您没有需要保留的数据,请先运行php artisan migrate:fresh。听起来您在数据库中有迁移记录。这将删除您的所有表格,因此请注意。

标签: php laravel migration


【解决方案1】:

Laravel 5.4 默认使用 utf8mb4。解决此错误的一种方法是默认返回 utf8。

转到您的 config\database.php 并将数据库字符集更改为 uft8 并将排序规则更改为 utf8_unicode_ci:

'mysql' => [
    //..
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    //..
],

【讨论】:

    【解决方案2】:

    打开 AppServiceProvider.php 编写以下代码:

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

    表名必须是复数 使用

    回滚您的迁移
    php artisan migrate:rollback
    

    从您的数据库和迁移表中手动删除该表 并尝试以下代码:

    migration command:
    php artisan make:migration create_currencies_table
    

    代码:

    class CreateCurrenciesTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('currencies', function (Blueprint $table) {
                $table->increments('id');
                $table->decimal('usdeur', 15, 2);
                $table->decimal('usdchf', 15, 2);
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('currencies');
        }
    }
    

    现在运行

    php artisan migrate
    

    【讨论】:

    • 还是不行。不知道我在这里缺少什么...我也做了composer du...
    【解决方案3】:

    所有的migrations记录都记录在表migrations中,说明是你之前创建的,所以删除表migrations中有关币种的记录,再次运行php artisan migrate。更重要的是,刷新您的数据库并检查表货币是否已创建。

    【讨论】:

      【解决方案4】:

      你需要先运行composer du来注册迁移类。

      然后运行php artisan migrate 命令执行新的迁移。

      【讨论】:

      • composer du 表示 composer dump-autoload ?
      • @SuYang 这是composer dump-autoload 的快捷方式。你可以同时使用。
      • 还是不行。现在真的很迷茫! :/.. 我明白了:SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
      • @harunB10 执行此操作后您是否遇到任何错误?另外,在执行迁移之前,请确保您没有 currency 表。
      猜你喜欢
      • 2018-02-06
      • 2014-03-31
      • 1970-01-01
      • 2016-09-25
      • 2016-07-11
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      相关资源
      最近更新 更多