代码之家  ›  专栏  ›  技术社区  ›  Vincent Decaux

Laravel-向表中添加字段

  •  0
  • Vincent Decaux  · 技术社区  · 7 年前

    向表中添加字段的方法是什么?我想在用户表中添加一些字段。所以我创建了一个迁移:

    php artisan make:migration add_data_users --table=users
    

    我在迁移中添加了字段。但是当我尝试迁移时,我得到了一个错误:

    基本表或视图已经存在:1050个表“权限”已经存在

    这个表已经被迁移了,但是迁移似乎重建了所有的模式(?)?!). 在Symfony中,我只向实体添加字段,这里,我需要创建迁移吗?然后,如何只应用新的迁移?我试图将迁移的名称添加到命令中,但它不起作用。

    编辑

    我好像有两次迁徙 表,仔细看一下您的迁移,我删除了第二个迁移文件,它就工作了。

    1 回复  |  直到 7 年前
        1
  •  -2
  •   AddWeb Solution Pvt Ltd    7 年前

    您应该创建新的迁移,如:

    php artisan make:migration update_users_table
    

    UpdateUsersTable.php更新 //你的新移民

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class UpdateUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->string('name', 36)->nullable()->default(null);
            });
        }
    

    :请为特定表运行迁移

    在migrations文件夹中创建测试文件夹之后,新创建的迁移在测试文件夹中移动/复制,并在终端/cmd中运行以下命令,如下所示:

    php artisan migrate --path=database/migrations/test/