【问题标题】:Laravel 9.x Terminal can not migrate tableLaravel 9.x 终端无法迁移表
【发布时间】:2022-07-19 17:20:42
【问题描述】:

正在尝试迁移用户表。 DB 已经有 1 张桌子。联系表格是Contacts。尝试迁移 users 表。使用终端 $ php artisan make:migration create_users_table
创建了我的表 里面有如下代码。

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('username')->unique();
            $table->timestamp('email_verified_at')();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
         });
    }

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

当我尝试迁移表终端时返回错误。

我运行 php artisan migrate 并返回 Migrating: create_contacts_table

“我正在尝试迁移 users 表,因为联系人表已经存在,所以它说 table already exists : 1050 ..."

试图按照网站上所说的那样给出一个论点,但也没有奏效。 我试过这个 $ php artisan migrate [--path[C:\xampp\htdocs\custom\database\migrations\2022_05_03_121341_create_users_table.php]]

但它返回“迁移”命令没有预期的参数

如何更改 migrate 命令的目录?或者我该如何解决这个问题。

【问题讨论】:

  • 迁移特定迁移使用以下命令完成:php artisan migrate --path=/database/migrations/full_migration_file_name_migration.php至于1050错误,请发布完整的迁移文件。
  • 您似乎还有一个 contracts 迁移,该迁移以前没有通过迁移运行(无论出于何种原因),但您已经创建了迁移。如果这只是开发环境并且您不关心数据丢失,请尝试运行php artisan migrate:fresh不要在生产环境中运行,或者如果您想将数据保留在数据库中
  • @geertjanknapen 我试过了,但它返回了这个错误是什么原因造成的? ibb.co/Fzn13Bc
  • @apokryfos 是的,我想我之前遇到过这个错误,但我能够将表迁移到数据库。这是一个开发环境,但我在 contacts 表中有大约 20-30 个数据。是清空所有表还是清空数据库?
  • @geertjanknapen 我编辑了问题并在我的迁移文件中添加了所有内容。

标签: php laravel artisan-migrate


【解决方案1】:

所以首先更改时间戳行和 把这一行:

$table->timestamp('email_verified_at')->nullable();

然后在命令行中使用:

php artisan migrate:fresh

【讨论】:

  • 谢谢这解决了我的问题,但我不明白它背后的逻辑。可空意味着可以为空或其中的数据对吗?那么我们为什么要这样做呢?
  • 我编辑了这个,现在可以了@NervousDev
【解决方案2】:
public function down()
{
    Schema::table('users', function($table) {
        $table->dropUnique(['email']);
        $table->dropColumn('email');
        $table->dropUnique(['username']);
        $table->dropColumn('username');
    });
    Schema::dropIfExists('users');
}

run-> php artisan migrate:refresh 并且不要忘记在 down 方法运行 add drop uniques 之前。

【讨论】:

    【解决方案3】:

    试试这个:

    php artisan migrate:refresh --path=/database/migrations/your_file_name.php
    

    【讨论】:

      猜你喜欢
      • 2019-12-27
      • 2018-04-23
      • 1970-01-01
      • 2016-04-12
      • 2018-08-01
      • 2019-11-06
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多