将列类型更改为tinyInteger

时间:2017-08-09 13:01:34

标签: php laravel migration 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()
    {
        //
    }
}

我收到错误:

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. 

我做错了吗?

7 个答案:

答案 0 :(得分:6)

确实,Doctrine Dbal不支持tinyint您可以从他们的文档中读取here

不幸的是,laravel表示tinyint无法更改。查看here

我需要有人证明这是错误的,因为我必须使用smallInteger因为我的一个项目的这个问题。我想也许boolean()可能是解决方案。我没有试过这个。

enter image description here

答案 1 :(得分:1)

你能用boolean吗?

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

答案 2 :(得分:1)

如果您尝试将非数字列转换为整型列,您将收到此错误。值无法转换。

在将旧字符串值转换为对父表的 id 引用时,您可能会遇到这种情况。

与其尝试更改现有列,不如创建一个新列并删除旧列:

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

答案 3 :(得分:0)

我希望这能解决你的问题

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

答案 4 :(得分:0)

执行此操作

  

将tinyInteger更改为smallInteger

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


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

答案 5 :(得分:0)

我遇到了同样的问题,发现了这个solution。它为我工作。但这给我提出了一个问题,即为什么创建者不更新到doctrine/dbal包。也许这种解决方案在某些情况下会导致错误?希望有人在这个答案中解释。

答案 6 :(得分:-2)

试试这个 Schema :: table(&#39; table_name&#39;,function(Blueprint $ table){       $表 - &GT; tinyInteger(&#39;列名&#39;) - &GT;默认(0) - &GT;变化();