在 Laravel 的迁移中向现有 table 添加新列

Add new columns to existing table in a migration in Laravel

我想在我现有的 table users laravel 中添加一些新列。

我已经在谷歌上搜索过了,按照这些搜索,我已经使用命令 php artisan make:migration add_columns_to_users.

创建了迁移

add_columns_to_users.php

public function up()
{
    Schema::table('users', function($table) {
        $table->string('address');
        $table->string('city');
        $table->string('tribe');
        $table->string('country');
        $table->integer('student_id');
        $table->string('tribe_university_name');
        $table->string('student_program_of_study');
        $table->string('faculty');
        $table->string('level');
    });
}

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('address');
        $table->dropColumn('city');
        $table->dropColumn('tribe');
        $table->dropColumn('country');
        $table->dropColumn('student_id');
        $table->dropColumn('tribe_university_name');
        $table->dropColumn('faculty');
        $table->dropColumn('level');
    });
}

创建后,我运行这个命令php artisan migrate

但是得到同样的错误:

Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8 collate utf8_unicode_ci)

用户的全名 table 2014_10_12_000000_create_users_table.php 另一个名字是 2019_04_11_074552_add_column_to_users.php

如何解决?

我的主要查询是 如何在现有 table 中添加新列?

修改当前迁移不起作用,因为它在 migration table 中的条目已经存在。因此,要对现有 table 进行任何更改,您需要创建新的迁移脚本。

// remember `create` replaced by `table` here  

Schema::table('users', function (Blueprint $table) { 
    // add whichever new columns you want to
});

按照这些步骤,

  1. php artisan make:migrate modify_user_table
  2. 打开 database/migrations 目录中的 modify_user_table 文件
  3. 添加我在顶部写的新列。

  4. 现在将新列添加到新迁移文件后保存文件

  5. cmd -> php artisan migrate

编辑

  1. 如果没有用户数据则打开2014_10_12_000000_create_users_table.php文件并在Schema::create('users'...行之前添加Schema::dropIfExists('users');

  2. 如果有数据可以备份,再按照上面的步骤1

请执行第 1 步。 php artisan migrate:reset 第 2 步:使用 PHPmyadmin(或类似工具)转到您的数据库并删除所有剩余的 tables - 包括迁移 table.

完成后请执行第 3 步 php artisan 迁移

如果检查错误跟踪:

Base table or view already exists: 1050 Table 'users' already exists

这意味着 用户 table 已经存在,所以当您 运行 迁移时,它正在尝试创建一个 table已在您的数据库中创建。

注意:别忘了先备份你的数据库

从数据库中删除 用户 table 同时从 migrations table.[=20 中删除用户条目=]

之后,执行migrate Artisan命令:php artisan migrate


现在您的另一个问题是:如何在现有 table 中添加新列?

您必须使用此命令创建 table:

php artisan make:migration create_users_table

你得到的输出是这样的:创建的迁移:2019_04_12_070152_create_users_table

你的迁移结构是这样的:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

现在您想在现有用户中添加新列table

php artisan make:migration add_phone_number_to_users_table --table=users

使用 Schema::table() 方法(因为您正在访问现有的 table,而不是创建新的)。您可以像这样添加一列:

public function up()
{
     Schema::table('users', function (Blueprint $table) {
         $table->string('phonenumber')->after('name'); // use this for field after specific column.
     });
}

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

之后,您可以 运行 您的迁移:php artisan migrate

您的新列 (phonenumber) 现已添加到您现有的 用户 table,您可以在您的数据库中查看它们。

如果还有疑问,请看这里video

问题来自 php artisan migrate,当 2014_10_12_000000_create_users_table.php 已经迁移时,它会尝试迁移两个文件,所以 IMO 这里有两个可能的解决方案:

  1. 从数据库回滚 users table 并重新运行 迁移命令。
  2. migrations table 中添加迁移名称,这样 cmd 将不会第二次尝试 运行 它。

您需要对您的 artisan 命令稍作修改

artisan make:migration add_columns_to_users_table

然后您需要使用 Schema::table() 方法(因为您正在访问现有的 table,而不是创建新的)。你可以像这样添加一列

  public function up()
  {
     Schema::table('users', function($table) {
          $table->type('column');
      });
   }

添加回滚选项:

 public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('column');
    });
 }

然后你可以运行你的迁移:

 php artisan migrate

您可能会遇到错误,因为您试图创建另一个 Users table,它已经存在于您的数据库中。这可能是您发现错误 Base table or view already exists: 1050 Table 'users' already exists 的原因。

因此,解决方案是尝试改变您现有的 Users table,而不是 运行 另一种语法来创建和覆盖 Users table。通过在数据迁移文件夹中创建另一个 alter class。它曾经位于 Laravel 项目 (../database/migrations/) 的 modules 文件夹中。

  • 找到目录后,创建新的alter class。例如,alter_users_table.php.
  • 因此,如果您想在 Users table 上添加另一列(例如:age 列,类型为 int,可为空),您可以在 alter_users_table.php:
  • 上这样的代码

class AlterUsersAddAge extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('Users', function (Blueprint $table) {
            $table->int('age')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('Users', function (Blueprint $table) {
            $table->dropColumn('age');
        });
    }
}
  • 之后,尝试在您的终端上 运行 php artisan migrate
  • 现在您应该会在 Users table 上看到另一个新列 age