【问题标题】:laravel 5.4 - php artisan migrate command doesn't worklaravel 5.4 - php artisan migrate 命令不起作用
【发布时间】:2017-10-10 04:49:12
【问题描述】:

我有四个迁移文件,当我在命令行中运行 php artisan migrate 时,它会显示:

Nothing to migrate

我也试过php artisan migrate --database=vcp:

Database [vcp] not configured.

我在我的.env 文件中使用了另一个数据库并再次运行php artisan migrate 命令:

Migration table created successfully.
Nothing to migrate. 

运行

php artisan migrate:refresh、php artisan migrate:reset、php artisan migrate:status、php artisan migrate --path="database/migrations/migration_file" 带有这些消息

Nothing to rollback.
Nothing to migrate.
No migrations found

和

composer dump-autoload or composer update

没有帮助。

这是我的.env 文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=vcp
DB_USERNAME=root
DB_PASSWORD=secret

我的2017_05_10_201750_add_columns_to_users.php

<?php

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

class AddColumnsToUsers extends Migration
   {
       /**
         * Run the migrations.
         *
         * @return void
        */
       public function up()
      {
            Schema::table('users', function (Blueprint $table) {
                $table->string('status')->default('online');
                $table->string('api_token');
           });
       }

     /**
      * Reverse the migrations.
      *
      * @return void
     */
    public function down()
     {
         Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('status');
            $table->dropColumn('api_token');
        });
     }
} 

请帮忙!

【问题讨论】:

  • 您的迁移文件是什么样的,您给它们起什么名字?
  • 我更改了数据库并再次运行 php artisan migrate 命令: 更改是在迁移文件中完成还是直接在数据库上完成?
  • 我用php artisan make:migration create_name_of_table创建了它们
  • 你试过php artisan migrate --path="database/migrations"吗?
  • @Mayank Pandeyz 直接在数据库上,除了迁移表和我的迁移表是空的,数据库中不存在任何内容!

标签: php mysql laravel laravel-artisan


【解决方案1】:

正如您在上面的 cmets 中所述,The changes are done directly on database。那么Nothing to migrate. 是正确的。

尝试理解这个概念,迁移包含文件中的表结构,并且当您运行迁移时,这些表是在数据库中创建的。当您对迁移文件进行更改时,您必须再次运行迁移,以便将这些更改反映回数据库表。所以流程是:

migration file -> database table

不是

database table -> migration file

【讨论】:

  • 我理解这个概念,谢谢!但我不知道我的代码哪里出了问题。我仍然无法迁移我的迁移文件!
【解决方案2】:

这对我有用:

首先,删除所有数据库表,包括迁移表。 其次,您的 AddColumnsToUsers 文件的更新方法 up() 如下所示:

public function up()
    {
    DB::beginTransaction();
    try {

        Schema::table('users', function (Blueprint $table) {
            $table->string('status')->default('online');
            $table->string('api_token');
       });

    DB::commit();
    } catch (PDOException $e) {
        DB::rollBack();
        $this->down();
    }
}

这将检查您的数据库以查看表是否已存在,如果表不存在则创建表,如果存在则回滚。 让我知道这是否有效。

【讨论】:

    【解决方案3】:

    有时您像我一样手动编写 Migration 函数,复制和粘贴旧函数,然后手动更改文件名。 前任: 文件 2021_10_13_082805_insert_fields_pages_table.php 如果情况相同,请注意类名,它必须包含反映文件名的名称。 在这个例子中:

    class InsertFieldsPagesTable extends Migration
    

    如果您忘记更改类名,迁移什么也不做。

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 2020-02-29
      • 1970-01-01
      • 2021-05-10
      • 2016-04-01
      • 1970-01-01
      • 2021-01-10
      • 2020-10-23
      • 2019-08-18
      相关资源
      最近更新 更多