【问题标题】:Laravel migration table field's type changeLaravel 迁移表字段的类型更改
【发布时间】:2016-01-01 15:08:52
【问题描述】:

以下是我的文件 2015_09_14_051851_create_orders_table.php。 我想将$table->integer('category_id'); 更改为带有新迁移的字符串。

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('num');
            $table->integer('user_id');

            $table->text('store_name');
            $table->integer('store_name_publication');

            $table->string('postal_code', 255);
            $table->string('phone_number', 255);

            $table->text('title');
            $table->text('description');

            $table->string('list_image_filename1', 255);
            $table->string('list_image_filename2', 255)->nullable();
            $table->string('list_image_filename3', 255)->nullable();
            $table->string('list_image_filename4', 255)->nullable();
            $table->string('list_image_filename5', 255)->nullable();

            $table->integer('term');

            $table->datetime('state0_at')->nullable();
            $table->datetime('state1_at')->nullable();
            $table->datetime('state2_at')->nullable();
            $table->datetime('state3_at')->nullable();
            $table->datetime('state4_at')->nullable();
            $table->datetime('state5_at')->nullable();
            $table->datetime('state6_at')->nullable();
            $table->datetime('state7_at')->nullable();
            $table->datetime('state8_at')->nullable();
            $table->datetime('state9_at')->nullable();
            $table->datetime('state10_at')->nullable();

            $table->integer('category_id');
            $table->integer('target_customer_sex');
            $table->integer('target_customer_age');

            $table->integer('payment_order');
            $table->integer('num_comment');
            $table->integer('num_view');
            $table->string('num_pop');

            $table->integer('money');
            $table->integer('point');

            $table->datetime('closed_at');
            $table->timestamps();
            $table->softDeletes();
        });
    }

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

}

【问题讨论】:

  • $table-&gt;string('category_id');
  • 不,我只想使用其他迁移文件 2015_10_05_021049_change_category_id_to_orders_table 更改该类型

标签: php laravel migration


【解决方案1】:

不是真正的答案,只是关于-&gt;change()的注释:

只有以下列类型可以“更改”:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger 和 unsignedSmallInteger。

https://laravel.com/docs/5.8/migrations#modifying-columns

如果您的列不是其中之一,您将需要删除该列或使用其他答案中提到的 alter 语句。

【讨论】:

    【解决方案2】:

    首先作曲家需要doctrine/dbal,然后:

    $table->longText('column_name')->change();
    

    【讨论】:

      【解决方案3】:

      所有其他答案都是正确的但是在你运行之前

      php artisan migrate
      

      确保先运行此代码

      composer require doctrine/dbal
      

      为了避免这个错误

      RuntimeException : 更改表“项目”的列需要 Doctrine DBAL;安装 “教义/dbal”。

      【讨论】:

        【解决方案4】:

        更新:2018 年 10 月 31 日,仍然可以在 laravel 5.7 上使用 https://laravel.com/docs/5.7/migrations#modifying-columns

        要对现有数据库进行一些更改,您可以在迁移中使用change() 修改列类型。

        这是你可以做的

        Schema::table('orders', function ($table) {
            $table->string('category_id')->change();
        });
        

        请注意你需要添加 doctrine/dbal 依赖到 composer.json 欲了解更多信息,你可以在这里找到它http://laravel.com/docs/5.1/migrations#modifying-columns

        【讨论】:

        • 现在我使用的是 laravel 4.2
        • 对于任何想知道的人来说仍然可以在 Laravel 5.5 中使用。 (因为这个答案已经有几年了)
        • 是的。你可以从这里查看Modifying Column(Laravel 5.5)
        • 如何将字符串更改为 int 并添加默认值?我试过了,但是 PostgreSQL 抛出了转换错误。 $table-&gt;integer('score')-&gt;default(0)-&gt;change();
        【解决方案5】:

        2018 解决方案,其他答案仍然有效,但您不需要使用任何依赖项

        首先你必须创建一个新的迁移:

        php artisan make:migration change_appointment_time_column_type
        

        然后在那个迁移文件up(),尝试:

            Schema::table('appointments', function ($table) {
                $table->string('time')->change();
            });
        

        如果您不更改大小默认值为varchar(191),但如果您想更改字段大小:

            Schema::table('appointments', function ($table) {
                $table->string('time', 40)->change();
            });
        

        然后通过以下方式迁移文件:

        php artisan migrate
        

        more info from doc.

        【讨论】:

        • 你还不需要使用“doctrine/dbal”依赖吗?
        • 在我看来,这是唯一正确的答案。由于迁移文件除了更改架构的主要目的之外,也是为数据库架构更改记录和版本控制的手段,因此单独的迁移文件在这里是必须的。
        • 你还需要doctrine/dbal
        • 必须有学说/dbal 包。
        【解决方案6】:

        对我来说,解决方案只是将 unsigned 替换为 index

        这是完整的代码:

            Schema::create('champions_overview',function (Blueprint $table){
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->integer('cid')->index();
                $table->longText('name');
            });
        
        
            Schema::create('champions_stats',function (Blueprint $table){
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->integer('championd_id')->index();
                $table->foreign('championd_id', 'ch_id')->references('cid')->on('champions_overview');
            });
        

        【讨论】:

          【解决方案7】:

          将类型从 TEXT 更改为 LONGTEXT 时,standard solution 对我不起作用。

          我不得不这样:

          public function up()
          {
              DB::statement('ALTER TABLE mytable MODIFY mycolumn  LONGTEXT;');
          }
          
          public function down()
          {
              DB::statement('ALTER TABLE mytable MODIFY mycolumn TEXT;');
          }
          

          这可能是一个教义问题。更多信息here

          另一种方法是使用string()方法,并将值设置为文本类型最大长度:

              Schema::table('mytable', function ($table) {
                  // Will set the type to LONGTEXT.
                  $table->string('mycolumn', 4294967295)->change();
              });
          

          【讨论】:

          • 这对 DECIMAL 列特别有用。
          • 我可以确认这种行为。为了将一列从 text 更改为 mediumtext 我必须这样做: $table->string('messages', 16777215)->nullable()->change();
          • $table-&gt;longText('mycolumn')-&gt;change();
          • 这是一个很好的解决方案,因为 Laravel 迁移不支持在同一张表上有另一个 ENUM 字段时更改任何其他字段类型。
          • 但无法运行测试
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-01
          • 1970-01-01
          • 2016-02-26
          • 2013-11-19
          • 2017-12-21
          • 2018-06-18
          相关资源
          最近更新 更多