【问题标题】:Laravel - When using make:migration created tables in database generates empty migration fileLaravel - 使用 make:migration 在数据库中创建的表时会生成空的迁移文件
【发布时间】:2020-02-21 09:09:51
【问题描述】:

我正在运行全新安装的 Laravel,其中包含干净的数据库和文件。

我创建了一个名为“frooth”的表,其中包含 id、title 和 created_at 列(id PK、varchar 和 datetime)

当我运行“php artisan make:migration frooth”命令时,创建的迁移文件是空的,只包含 up() 和 down() 函数,仅此而已(没有列)

我该如何解决这个问题,我按照官网介绍的框架的基本配置,我可以按预期访问和创建artisan中的功能,只是迁移它不起作用。

我使用命令生成项目:composer create-project --prefer-dist laravel/laravel blog

create table laravel.frooth
(
    id         int auto_increment
        primary key,
    title      varchar(250) null,
    created_at datetime     null
);

database/migrations/2019_10_25_012925_frooth.php中生成的PHP类:

<?php

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

class Frooth extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

控制台输出:

php artisan make:migration frooth
Created Migration: 2019_10_25_012925_frooth

【问题讨论】:

  • 不要手动创建表。使用您的迁移文件来制作表格。将列添加到迁移表后,保存文件并运行:php artisan migratelaravel.com/docs/5.8/migrations#generating-migrations
  • 当你有一个现有的数据库并想为它创建一个新的 laravel 项目时,你可以使用github.com/Xethron/migrations-generator 来生成所需的迁移文件
  • @JulianStark,不幸的是 Xethron/migrations-generator 与这个版本的 Laravel 不兼容,但我会测试其他版本的 Laravel 包。
  • @echo 在这种情况下,我正在考虑在这个项目中使用 Laravel Voyager(它从其管理界面创建数据库,而不是在源代码中创建每个数据库。但我期待着测试它也是。

标签: php laravel mariadb laravel-migrations laravel-6.2


【解决方案1】:

删除您手动创建的表并删除该迁移文件。

运行php artisan make:migration create_frooths_table

然后将您的列添加到新的迁移文件中。

类似的东西:

<?php

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

class Frooth extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('frooths', function (Blueprint $table) {

        $table->increments('id');
        $table->string('title')->nullable();
        $table->timestamps();
          }); 
        }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('frooths');
    }
}

然后运行php artisan migrate

对于id,如果使用 Laravel 6,您可能需要使用 $table-&gt;bigIncrements('id');

【讨论】:

    猜你喜欢
    • 2017-05-19
    • 2017-06-15
    • 2018-10-26
    • 2016-02-28
    • 2016-06-09
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多