将列类型更改为tinyInteger

mpe*_*pet 3 php migration laravel laravel-5.2

在Laravel 5.2迁移中尝试将数据列类型更改为tinyInteger:

<?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()
    {
        //
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

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. 
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

Olu*_*kin 8

确实,Doctrine Dbal不支持tinyint您可以在此处阅读其文档

同样不幸的是,laravel表示tinyint无法更改。在这里检查

我需要有人证明这是错误的,因为我的一个项目由于这个问题不得不使用smallInteger。我在想也许boolean()是解决方案。我还没有尝试过。

在此处输入图片说明


ric*_*ian 5

我希望这能解决你的问题

DB::statement("ALTER TABLE table_name CHANGE COLUMN column_name column_name TINYINT UNSIGNED NOT NULL");
Run Code Online (Sandbox Code Playgroud)

  • 这可能是公认的答案!例如,当您想要使用 0 到 9(或最多 255)的数字时,布尔解决方案就不合适。 (2认同)