mpet
mpet

Reputation: 1014

Change column type to tinyInteger

Trying to change data column type to tinyInteger in a Laravel 5.2 migration:

<?php

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

class AlterTableNameTableChangeNotificationSentTinyint extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('table_name', function ($table) {
            $table->tinyInteger('column_name')->default(0)->change();
        });    
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

I'm getting an error:

Doctrine\DBAL\DBALException]                                                                                                                                                              
  Unknown column type "tinyinteger" 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 wit  
  h \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use Abstrac  
  tPlatform#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 so  
  me mapping information. 

Am I doing something wrong?

Upvotes: 27

Views: 28925

Answers (9)

Masaba James Moses
Masaba James Moses

Reputation: 602

This should work for you, Just change from tinyInteger to smallInteger as below;

<?php

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

class AlterTableNameTableChangeNotificationSentTinyint extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('table_name', function ($table) {
            $table->smallInteger('column_name')->default(0)->change();
        });    
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Upvotes: 1

Amit Shah
Amit Shah

Reputation: 8179

!!! This solution is only for empty tables. Not if already populated.

Just drop and recreate the column with same name.

    public function up()
    {
        // Drop and recreate because laravel don't allow to change to the tinyInteger type 
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->dropColumn(['rating']);
        });

        Schema::table('your_table_name', function (Blueprint $table) {
            $table->tinyInteger('rating')->nullable()->after('some_column_name');
        });
    }

Upvotes: 0

RicardoPHP
RicardoPHP

Reputation: 564

According to this https://github.com/laravel/framework/issues/8840 "BOOL" and "BOOLEAN" are both synonymous to "TINYINT" therefor just use "boolean" method instead of "tinyInteger", its the same in Laravel.

Upvotes: -2

Matt Rabe
Matt Rabe

Reputation: 2065

If you are trying to convert a non-numeric column to an int column, you will get this error. The values cannot be converted.

You might run into this when converting an old string value to an id reference to a parent table.

Instead of trying to change the existing column, create a new column and delete the old:

// Add new int column
Schema::table('children', function (Blueprint $table) {
    $table->unsignedTinyInteger('parent_id')->after('parent_slug');
});

// Convert old values to new
// Only runs on environments that already have data in db, by virtue of pulling all records from the parents table
foreach (\App\Parents::all() as $parent) {
    \App\Child::where('parent_slug', $parent->slug)->each(function ($child) use ($parent) {
        $child->update([ 'parent_id' => $parent->id ]);
    });
}

// Drop old string column
Schema::table('children', function (Blueprint $table) {
    $table->dropColumn('parent_slug');
});

Upvotes: 0

Harry Bosh
Harry Bosh

Reputation: 3790

Can you use boolean?

or

$table->smallInteger('column_name')->tinyInteger('column_name')->unsigned()->change();

Upvotes: 1

Kumar Subedi
Kumar Subedi

Reputation: 354

Do This

Change tinyInteger to smallInteger

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\SmallIntType;


if (!Type::hasType('integer')) {
     Type::addType('integer', SmallIntType::class);
  }

Upvotes: 4

omitobi
omitobi

Reputation: 7334

Indeed Doctrine Dbal does not support tinyint you can read from their doc here

Unfortunately as well, laravel stated that tinyint cannot be changed. Check here

I need someone to prove this as wrong, because I had to use smallInteger because of this issue for one of my projects. I am thinking maybe boolean() might be the solution. I have not tried this though.

enter image description here

Upvotes: 34

ricristian
ricristian

Reputation: 536

i hope that this will solve your issue

DB::statement("ALTER TABLE table_name CHANGE COLUMN column_name column_name TINYINT UNSIGNED NOT NULL");

Upvotes: 21

Nikita
Nikita

Reputation: 437

try this Schema::table('table_name', function (Blueprint $table) { $table->tinyInteger('column_name')->default(0)->change();

Upvotes: -6

Related Questions