如何编写迁移文件以添加字段类型'ltree‘(PostgreSQL)
Schema::create('table', function (Blueprint $table) {
....
$table->ltree('path');
}不管用。
谢谢!
发布于 2016-12-20 21:15:55
查看可用函数的手册:https://laravel.com/docs/5.1/migrations#creating-columns
Laravel的目标是兼容性,所以除非在所有受支持的数据库中都有相同的结构,否则它们不太可能在本地支持它。
可以使用DB::statement('CREATE TABLE ...')手动运行SQL语句。
请记住,您的应用程序将锁定到postgres,这可能并不理想。
发布于 2018-10-20 12:29:33
作为快速解决方案,在迁移中使用此方法:
public function up ()
{
Schema::create('locations', function (Blueprint $table) {
$table->increments('id');
$table->uuid('uuid')->unique();
$table->string('path', 255); // <--- my ltree field
$table->timestamps();
});
$query = 'ALTER TABLE locations ALTER COLUMN path TYPE "ltree" USING "path"::"ltree";';
\Illuminate\Support\Facades\DB::connection()->getPdo()->exec($query);
}发布于 2022-02-04 06:06:34
这里business_category是我的ltree数据类型
public function up()
{
Schema::create('business_categories', function (Blueprint $table) {
$table->increments('business_category_id')->generatedAs();
$table->string('business_category')->nullable();
$table->timestamps();
});
DB::statement("ALTER TABLE business_categories ADD COLUMN business_category_path ltree");
DB::statement("CREATE INDEX business_categories_business_category_path_gist_idx ON business_categories USING gist(business_category_path)");
DB::statement("CREATE INDEX business_categories_business_category_path_idx ON business_categories USING btree(business_category_path)");
}此外,如果未启用ltree扩展,则必须启用它。
try {
DB::statement("create extension ltree");
} catch (\Throwable $e) {
print "ltree extension already exist";
}https://stackoverflow.com/questions/41251284
复制相似问题