在 Laravel 迁移中,我应该使用什么作为 Point
类型列的默认值?我本来想保留它 NULL
但后来我 read那:
Columns in spatial indexes must be declared NOT NULL.
那么我应该使用什么作为列的默认值以及如何在迁移中指定它来表示 NULL
,例如 0,0
或 -1,-1
?
$table->point('location')->default(???);
更新
做更多的研究,我发现了一个更大的问题。 MySQL 不允许为 POINT
类型的列指定默认值。所以我必须在 INSERT
时插入一个 NULL
等价物。为此,正确的值是多少?
最佳答案
我不知道你是不是这种情况,但如果你想在现有表中添加一列,然后在迁移时向该表添加空间索引,我发现在 MySQL 中解决这个问题的方法是不是一个优雅的解决方案,但有效:
Schema::table('table', function(Blueprint $table)
{
// Add the new nullable column
$table->point('column')->nullable();
});
// You must separate this to ensure the execution order
Schema::table('table', function(Blueprint $table)
{
// Insert the dummy values on the column
DB::statement("UPDATE `table` SET `column` = POINT(0,90);");
// Set the column to not null
DB::statement("ALTER TABLE `table` CHANGE `column` `column` POINT NOT NULL;");
// Finally add the spatial index
$table->spatialIndex('column');
});
关于mysql - 点类型的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52510390/