簡體   English   中英

Laravel 5.4 遷移 ENUM 在 MySQL 中失敗

[英]Laravel 5.4 migration ENUM fails in MySQL

當我嘗試應用遷移時,出現此錯誤:

[Doctrine\DBAL\DBALException]
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it.

應用了遷移,在數據庫上創建了 enum 列,但出現錯誤,因此我無法執行 nexts 遷移,因為此遷移會引發此錯誤。

在服務器中,我有 MySQL 版本 5.7.17

這是我的遷移代碼:

class AddDocumentUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('document', 9)->unique();
            $table->enum('document_type', ['dni', 'nie', 'nif', 'cif']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('document');
            $table->dropColumn('document_type');
        });
    }
}

謝謝 ;)

可以在此處找到與 laravel 嚴格相關的信息。 我強烈建議您閱讀該主題。 這不是 Laravel 問題,它一直是 Doctrine 中的一個錯誤。

從上面的問題線程,用戶henritoivar有一個有趣的想法。

引用這里:

這在 laravel 5.2 中對我有用,帶有 dotric/dbal@^2.5 。 當您的表上有一個枚舉並且您想要更改表上的任何列時,您必須:

public function up()
{
   Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

    Schema::table('jobs', function(Blueprint $table)
    {
        $table->decimal('latitude', 10, 6)->nullable()->change();
    });
}

我不知道這是否適合你,但值得一試。


我會把它作為評論發布,但它是一個該死的長讀物。

來自官方Doctrine文件

Doctrine 2 的類型系統由享元組成,這意味着任何給定類型只有一個實例。 另外類型不包含狀態。 這兩個假設使得使用開發人員大量使用的 MySQL 枚舉類型變得相當復雜。 在未調整的 Doctrine 2 應用程序中使用 Enum 時,由於未知的數據庫類型“enum”,您將從 Schema-Tool 命令中得到錯誤。 默認情況下,Doctrine 不會將 MySQL 枚舉類型映射到 Doctrine 類型。 這是因為 Enum 包含狀態(它們的允許值)而 Doctrine 類型不包含。

從技術上講,這是可以解決的。 這里 但這與 Laravel 所基於的 symfony 嚴格相關。


Laravel 的文檔還指出它有enums的問題

>當前不支持重命名表中還具有枚舉類型列的任何列。


雖然這不是答案,但我希望它為您指明正確的方向,或者至少讓您了解您所面臨的問題。


更多相關問題:

如何在 Symfony 2 / Doctrine 中啟用 ENUM

Laravel 5.1 請求的未知數據庫類型枚舉

在包含 ENUM 類型的遷移文件中,添加具有以下內容的構造函數方法:

public function __construct() {
    // Register ENUM type
    DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
}

這在 Laravel 5.2 中對我有用。 您可以嘗試在更高級別添加它,但這對我來說更快:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM