我在 Laravel 4 的 Migrations 中使用 Schema Builder 在开发过程中管理我的 Postgres 数据库。我遇到了问题。我想在表中使用 Postgres 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
。
最佳答案
这可以使用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 函数。如需完整的实现细节,请查看 here
关于php - 如何在 Laravel 4 中创建 Hstore 列类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19079840/