mysql - 点类型的默认值

标签 mysql laravel eloquent geospatial

在 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/

相关文章:

mysql - 将属性附加到 MySQL 中的 JSON 对象

php - 如何使用 Laravel Input::replace() 测试 POST 请求

php - Laravel 错误 - Connection.php 第 729 行中的 QueryException :

eloquent - Laravel 5 Eloquent - where (status = 1 or status = 2)

php - 如何在 Laravel 关系中使用 where 查询?

php - Laravel Eloquent : keeping all interactions with a model in sync in two different databases

mysql - SQL 用户搜索查询

mysql - 从一个 sql 中的第二个表获取内容标签

mysql - 您什么时候希望主键与其他列一起建立索引?

laravel - 将现有列更改为在迁移中具有默认值