2013-09-29 37 views
2

我在開發過程中使用Laravel 4中的遷移內部的Schema Builder來管理我的Postgres數據庫。我遇到了一個問題。我想在表中使用Postgres Hstore列類型,但我無法弄清楚。如何在Laravel 4中創建Hstore列類型?

架構生成器中似乎沒有「自定義」列類型(我可以找到),所以這是我在遷移過程中的嘗試。這對我不起作用。

Schema::create('entities', function(Blueprint $table) 
{ 
    $table->bigIncrements('id'); 
    $table->string('type'); 
    $table->string('name'); 
    $table->text('data')->nullable(); 
    $table->timestamps(); 
    $table->softDeletes(); 
}); 
DB::raw("ALTER TABLE `entities` CHANGE COLUMN `data` `data` hstore NULL;"); 

我也試過這個而不是最後一條命令。沒有運氣。

DB::raw("ALTER TABLE `entities` ALTER `data` TYPE hstore;"); 

注:我已經在我的數據庫中運行命令CREATE EXTENSION hstore

+1

我能夠得到它使用以下命令工作(看起來我有錯誤的方法,並關閉了SQL)。 'DB :: statement(「ALTER TABLE entities ALTER attributes TYPE hstore USING(attributes :: hstore);」);' 如果Laravel結合了自定義列類型,那就太好了。 – stevenwadejr

回答

2

這可以通過使用Builder::blueprintResolver功能

$builder = DB::connection()->getSchemaBuilder(); 

    $builder->blueprintResolver(function($table, $callback) { 
     return new CustomPostgresBlueprint($table, $callback); 
    }); 

    $builder->create('record_sets', function(CustomPostgresBlueprint $table) { 
     $table->increments('id'); 
     $table->hstore('schema'); 
     $table->timestamps(); 
    }); 

您還需要自定義PostgresGrammer子類中實現typeHstore函數來完成。 FOr完整的實現細節,看看here

相關問題