简体   繁体   中英

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?

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.

在此处输入图像描述

我希望这能解决你的问题

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

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

I got same problem and found this solution . It worked for me. But it raise in me a question that why creator don't update to doctrine/dbal package. Maybe this solution can cause errors in some case? Hope someone explain in this answer.

Can you use boolean ?

or

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

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

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

根据这个https://github.com/laravel/framework/issues/8840“BOOL ”和“BOOLEAN”都是“TINYINT”的同义词,因此只需使用“boolean”方法而不是“tinyInteger”,它在Laravel中是一样的.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM