当前位置: 首页 > 知识库问答 >
问题:

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

谭京
2023-03-14

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

我已经在谷歌上搜索过了,在这些搜索之后,我已经使用命令php-artisan-make:migration-add\u-columns\u-to\u-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

但也犯了同样的错误:

基本表或视图已存在:1050个表“用户”已存在(SQL:create tableusersidint unsigned not null自动递增主键,namevarchar(255)not null,emailvarchar(255)not null,varchar(255)not null,记住令牌varchar(100)空,时间戳空时创建,时间戳空时更新)默认字符集utf8 collate utf8\U unicode\U ci)

用户表的全名2014\u 10\u 12\u000000\u创建用户表。php另一个名称是2019\u 04\u 11\u 074552\u向用户添加列。php

如何解决这个问题?

我的主要问题是如何在现有表中添加新列?


共有3个答案

锺宜
2023-03-14

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

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

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

按照这些步骤,

>

  • php artisan make:migrate modify_user_table
  • 数据库/迁移目录中打开修改用户表文件
  • 在我写的顶部添加新列。

    现在,在向新迁移文件中添加新列后保存该文件

    编辑

    >

    如果有数据,则可以进行备份,再次执行上述步骤1。

  • 司马作人
    2023-03-14

    问题来自php artisan migrate,它将在2014\u 10\u 12\u000000\u创建用户表时尝试迁移这两个文件。php已经迁移,因此我在这里提供了两种可能的解决方案

    1. 从数据库回滚users表,然后重新运行migrate cmd
    景育
    2023-03-14

    如果在错误跟踪处进行检查:

    基表或视图已经存在: 1050表'用户'已经存在

    这意味着users表已经存在,因此当您运行迁移时,它将尝试创建一个已经在数据库中创建的表。

    注意:不要忘记先备份数据库

    从数据库中删除用户表还从迁移表中删除用户条目。

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

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

    您必须使用以下命令创建一个表:

    php artisan make:migration create_users_table
    

    您得到的输出如下:创建迁移:2019\u 04\u 12\u 070152\u创建用户\u表

    您的迁移结构如下所示:

    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();
            });
        }
    

    现在要在现有用户表中添加新列

    php artisan make:migration add_phone_number_to_users_table --table=users
    

    使用Schema::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迁移

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

    如果你还有任何疑问,请看这个视频

     类似资料:
    • 我不知道如何使用Laravel框架将新列添加到我现有的数据库表中。 我尝试使用...编辑迁移文件。 在终端中,我执行和。 如何添加新列?

    • 我不知道如何使用Laravel框架将新列添加到现有的数据库表中。 我试图使用…编辑迁移文件。。。 在终端,我执行和。 如何添加新列?

    • 我有一些表,我想在这个表中添加新的列。我想添加默认值为空的字符串列。 我试着这样加上去 或 但我有一个错误

    • 问题内容: 我不知道如何使用Laravel框架向现有数据库表添加新列。 我试图使用…来编辑迁移文件。 在终端中,我执行和。 如何添加新列? 问题答案: 要创建迁移,您可以在Artisan CLI上使用migration:make命令。使用特定名称以避免与现有模型冲突 对于Laravel 3: 对于Laravel 5+: 然后,您需要使用该方法(在访问现有表而不是创建新表时)。您可以添加如下所示的列

    • 我正在表任务中添加新列名标题。但我得到一个错误,该表中不存在此列。谁能帮我解决那个错误。这是我的密码: 然后添加此代码 到创建的新表文件

    • 我在Laravel4.2项目中工作,现在我想在现有的表用户中添加列。现在我想添加另一列,当我运行migration命令时,我总是收到相同的消息“无需迁移”下面是我的迁移模式代码 我在终点站跑步 我还运行以下命令 但是当我运行上面的命令时,我收到以下错误 SQLSTATE[42S01]:基表或视图已经存在: 1050表'用户'已经存在