dongying9756 2018-12-04 10:20
浏览 46
已采纳

生产迁移新鲜

How does it work if I want to add a new column in my production table? I know that I will loose all the data when I make a migration:fresh.

1) is it possible to change the migration and add a new column as I did with the fastnetnr column?

 public function up()
    {
        Schema::create('kontaktforms', function (Blueprint $table) {
            $table->increments('id');
            $table->string('navn');
            $table->string('mobilnr');
            //new fastnetnr column added
            $table->string('fastnetnr')->nullable();
            $table->string('mail');
            $table->string('emne');
            $table->text('beskrivelse');
            $table->timestamps();
        });

2) Or do I have to add a new column with php artisan so the output is like this?

public function up()
{
    Schema::table('kontaktforms', function($table) {
        $table->string('fastnetnr')->nullable();
    });
}

</div>
  • 写回答

1条回答 默认 最新

  • donglu4159 2018-12-04 11:14
    关注

    Logically you can do it by following steps;

    1. rollback your table php artisan migrate:rollback or php artisan migrate:rollback --step=1
    2. edit your migration file
    3. migration back php artisan migrate

    but it's not a best practice, especially in production. so you have to create new migration file for modify table and migrate it.

    create new migration file

    php artisan make:migration add_fastnetnr_to_kontaktforms_table --table=kontaktforms
    

    and add new column like this

    public function up()
    {
        Schema::table('kontaktforms', function (Blueprint $table) {
            $table->string('fastnetnr')->nullable();
        });
    }
    
    
    public function down()
    {
        Schema::table('kontaktforms', function (Blueprint $table) {
            $table->dropColumn('fastnetnr');
        });
    }
    

    and migrate

    php artisan migrate
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程