Search code examples
laraveleloquentlaravel-9laravel-migrations

Laravel migration to change an unsignedMediumInteger column


I have a need to create a migration that changes an existing 'unsignedMediumInteger' column to nullable().

I have the following in my migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use AppNew\Models\SubscriptionType;

return new class extends Migration
{
    public function up()
    {
        Schema::table('subscription_types', function ($table) {
            $table->unsignedMediumInteger('max_number_of_employees')->nullable()->change();
        });
    }
};

This throws an error as below:

  INFO  Running migrations.  

  2023_10_25_103141_make_subscription_types_max_number_of_employees_field_nullable  108ms FAIL

   Doctrine\DBAL\Exception 

  Unknown column type "mediuminteger" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

...any pointers as to get around this please?

Thanks, K...


Solution

  • Try this approach:

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Support\Facades\DB;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            DB::statement("ALTER TABLE <table_name> MODIFY COLUMN <column_name> MEDIUMINT UNSIGNED NULL");
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            DB::statement("ALTER TABLE <table_name> MODIFY COLUMN <column_name> MEDIUMINT UNSIGNED NOT NULL");
        }
    };