ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – 如何使用laravel 5迁移在表中添加列而不丢失其数据?

2019-10-04 01:29:28  阅读:208  来源: 互联网

标签:php laravel-5-2 postgresql migration


我有一个现有的数据库表,我想在其上添加列.但是,当我运行php artisan migrate命令时,它没有说任何迁移.但是我已经添加了一个用于添加表列的Schema.我已经阅读了一些文章和链接,我应该运行php artisan migrate:在添加新列之前先刷新.问题是,它将擦除我表中的现有数据.有没有什么办法可以执行迁移并在我的表中成功添加列而不删除我的数据?请帮我解决一下这个.非常感谢.这是我的迁移代码.

public function up()
{
    //
    Schema::create('purchase_orders', function(Blueprint $table){

        $table->increments('id');
        $table->string('po_code');
        $table->text('purchase_orders');
        $table->float('freight_charge');
        $table->float('overall_total');
        $table->timestamps();

    });

    Schema::table('purchase_orders', function(Blueprint $table){
        $table->string('shipped_via');
        $table->string('terms');
    });
}

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

我想在purchase_orders表中添加列shipping_via和条款.

解决方法:

使用以下命令修改现有表

php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders

use –create用于创建新表, – table用于修改现有表.

现在将创建一个新的迁移文件.在此文件中的up()函数内添加这些行

Schema::table('purchase_orders', function(Blueprint $table){
    $table->string('shipped_via');
    $table->string('terms');
});

然后运行php artisan migrate

标签:php,laravel-5-2,postgresql,migration
来源: https://codeday.me/bug/20191004/1851446.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有