简体   繁体   English

将列类型更改为 tinyInteger

[英]Change column type to tinyInteger

Trying to change data column type to tinyInteger in a Laravel 5.2 migration:尝试在 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()
    {
        //
    }
}

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事实上,Doctrine Dbal 不支持tinyint ,您可以在此处从他们的文档中阅读

Unfortunately as well, laravel stated that tinyint cannot be changed.不幸的是,laravel 声明tinyint不能更改。 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.我需要有人来证明这是错误的,因为我的一个项目因为这个问题而不得不使用 smallInteger。 I am thinking maybe boolean() might be the solution.我在想也许boolean()可能是解决方案。 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将 tinyInteger 更改为 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.但这向我提出了一个问题,即为什么创建者不更新到doctrine/dbal包。 Maybe this solution can cause errors in some case?也许这个解决方案在某些情况下会导致错误? Hope someone explain in this answer.希望有人在这个答案中解释。

Can you use boolean ?你可以使用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.如果您尝试将非数字列转换为 int 列,则会收到此错误。 The values cannot be converted.这些值无法转换。

You might run into this when converting an old string value to an id reference to a parent table.在将旧字符串值转换为对父表的 id 引用时,您可能会遇到这种情况。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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