簡體   English   中英

將列類型更改為 tinyInteger

[英]Change column type to tinyInteger

嘗試在 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. 

難道我做錯了什么?

事實上,Doctrine Dbal 不支持tinyint ,您可以在此處從他們的文檔中閱讀

不幸的是,laravel 聲明tinyint不能更改。 在這里檢查

我需要有人來證明這是錯誤的,因為我的一個項目因為這個問題而不得不使用 smallInteger。 我在想也許boolean()可能是解決方案。 我還沒有嘗試過。

在此處輸入圖像描述

我希望這能解決你的問題

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

做這個

將 tinyInteger 更改為 smallInteger

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


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

我遇到了同樣的問題並找到了這個解決方案 它對我有用。 但這向我提出了一個問題,即為什么創建者不更新到doctrine/dbal包。 也許這個解決方案在某些情況下會導致錯誤? 希望有人在這個答案中解釋。

你可以使用boolean嗎?

或者

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

如果您嘗試將非數字列轉換為 int 列,則會收到此錯誤。 這些值無法轉換。

在將舊字符串值轉換為對父表的 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');
});

!!! 此解決方案僅適用於空表。 如果已經填充,則不會。

只需刪除並重新創建具有相同名稱的列。

    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