php Laravel 迁移表字段的类型更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32940495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:17:03  来源:igfitidea点击:

Laravel migration table field's type change

phplaravelmigration

提问by naing linhtut

Following is my file 2015_09_14_051851_create_orders_table.php. And I want to change $table->integer('category_id');as a string with new migration.

以下是我的文件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');
    }

}

回答by mininoz

update: 31 Oct 2018, Still usable on laravel 5.7 https://laravel.com/docs/5.7/migrations#modifying-columns

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

To make some change to existing db, you can modify column type by using change()in migration.

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

This is what you could do

这是你可以做的

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

please note you need to add doctrine/dbaldependency to composer.json for more information you can find it here http://laravel.com/docs/5.1/migrations#modifying-columns

请注意,您需要在composer.json 中添加Doctic/dbal依赖项以获取更多信息,您可以在此处找到它http://laravel.com/docs/5.1/migrations#modifying-columns

回答by Francisco Corrales Morales

The standard solutiondidn't work for me, when changing the type from TEXTto LONGTEXT.

将类型从TEXT更改为LONGTEXT时,标准解决方案对我不起作用

I had to it like this:

我不得不这样做:

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

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

This could be a Doctrine issue. More information here.

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

Another way to do it is to use the string() method, and set the value to the text type max length:

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

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

回答by Blasanka

2018 Solution, still other answers are valid but you dont need to use any dependency:

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

First you have to create a new migration:

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

php artisan make:migration change_appointment_time_column_type

Then in that migration file up(), try:

然后在该迁移文件中up(),尝试:

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

If you donot change the size default will be varchar(191)but If you want to change size of the field:

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

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

Then migrate the file by:

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

php artisan migrate

more info from doc.

来自 doc 的更多信息

回答by Hussam Adil

all other answers are Correct ButBefore you run

所有其他答案都是正确的,在你跑步之前

php artisan migrate

make sure you run this code first

确保您先运行此代码

composer require doctrine/dbal

to avoid this error

为了避免这个错误

RuntimeException : Changing columns for table "items" requires Doctrine DBAL; install "doctrine/dbal".

RuntimeException :更改表“items”的列需要 Doctrine DBAL;安装“学说/dbal”。

回答by Mehdi Kord

First composer requires doctrine/dbal, then:

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

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

回答by lewis

Not really an answer, but just a note about ->change():

不是真正的答案,而只是关于->change()

Only the following column types can be "changed": bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger and unsignedSmallInteger.

只能“更改”以下列类型: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

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

If your column isn't one of these you will need to either drop the column or use the alter statement as mentioned in other answers.

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

回答by Ahmed Safadi

For me the solution was just replace unsignedwith index

对我来说,解决方案只是用索引替换无符号

This is the full code:

这是完整的代码:

    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');
    });