Laravel migration table field's type change

后端 未结 7 1479
难免孤独
难免孤独 2020-12-08 12:45

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 migratio

相关标签:
7条回答
  • 2020-12-08 13:19

    The standard solution didn't work for me, when changing the type from TEXT to 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:

        Schema::table('mytable', function ($table) {
            // Will set the type to LONGTEXT.
            $table->string('mycolumn', 4294967295)->change();
        });
    
    0 讨论(0)
  • 2020-12-08 13:21

    First composer requires doctrine/dbal, then:

    $table->longText('column_name')->change();
    
    0 讨论(0)
  • 2020-12-08 13:30

    update: 31 Oct 2018, Still usable on 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.

    This is what you could do

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

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

    0 讨论(0)
  • 2020-12-08 13:31

    Not really an answer, but just a note about ->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.

    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.

    0 讨论(0)
  • 2020-12-08 13:38

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

    First you have to create a new migration:

    php artisan make:migration change_appointment_time_column_type
    

    Then in that migration file up(), try:

        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:

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

    Then migrate the file by:

    php artisan migrate
    

    more info from doc.

    0 讨论(0)
  • 2020-12-08 13:42

    all other answers are Correct But Before 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".

    0 讨论(0)
提交回复
热议问题