简体   繁体   English

如何通过Laravel迁移向数据库表添加一列?

[英]How to add one column to a DB table with Laravel migrations?

I'm using Laravel migration files to help set up a MariaDB schema for my project. 我正在使用Laravel迁移文件来帮助为我的项目设置MariaDB模式。 It works fine for the initial table setup of the schema, but after it has been created, how (if it's even possible at all) can I add one field to a migration file and have just that one field be created in the DB without affecting any of the existing fields/data? 它适用于架构的初始表设置,但是在创建它之后,如何(如果可能的话)如何向迁移文件中添加一个字段,并在数据库中仅创建一个字段而不会影响任何现有的字段/数据?

As an example, let's say I have an up method like the following: 例如,假设我有一个如下所示的up方法:

public function up()
{
    Schema::create('test_table', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

And then I want to add a desc field as follows: 然后,我想添加一个desc字段,如下所示:

public function up()
{
    Schema::create('test_table', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('desc')->nullable();
        $table->timestamps();
    });
}

If I run php artisan migrate after adding the desc field, will it work as intended, or will it mess up pre-existing stuff? 如果我在添加desc字段后运行php artisan migrate desc ,它将按预期工作,还是会弄乱现有的东西? I'm afraid to try without knowing what will happen, and I wanted to check first. 我不敢尝试不知道会发生什么,我想首先检查。

Also, I tried looking at the Laravel documentation for an answer to this question ( https://laravel.com/docs/5.7/migrations ), but couldn't find anything relevant. 另外,我尝试查看Laravel文档以找到此问题的答案( https://laravel.com/docs/5.7/migrations ),但是找不到任何相关的内容。
Thanks. 谢谢。

Normally, you should create new migration where you just add new field. 通常情况下,你应该创建新的迁移,你只需要添加新的领域。 Method up should look like this: 方法up应该是这样的:

public function up()
{
    Schema::table('test_table', function (Blueprint $table) {
        $table->string('desc')->nullable()->after('name');
    });
}

Notice that you don't use here Schema::create but Schema::table to update existing table. 你不要在这里使用的通知Schema::create ,但Schema::table来更新现有的表。

Of course if you haven't deployed your app and can start from scratch you can modify existing migration, but assuming you are not sure, the best way is to create new migration where you only modify existing table. 当然,如果您尚未部署应用程序并且可以从头开始,则可以修改现有迁移,但是如果您不确定,最好的方法是在仅修改现有表的情况下创建新迁移。

You should do this in terminal run command 您应该在终端运行命令中执行此操作

php artisan make:migration add_desc_field_in_test_table --table=test_table

Then, in the up() function in your migration file, create the field: 然后,在migration文件的up()函数中,创建字段:

public function up()
{
    Schema::table('test_table', function (Blueprint $table) {
        $table->string('desc')->nullable();
    });
}

It will not mess the current table structure. 它不会弄乱当前的表结构。 It will just add the column desc in test_table . 它将仅在test_table 添加 desc列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM