In Laravel migrations, what should I use as the default value of Point
type column? I originally wanted to keep it NULL
but then I read that:
Columns in spatial indexes must be declared NOT NULL.
So what should I use as the default value of my column and how do I specify that in migration, to represent NULL
, something like 0,0
or -1,-1
?
$table->point('location')->default(???);
UPDATE
Doing more research, I found an even bigger issue. MySQL doesn't allow specifying default value for POINT
type column. So I must insert a NULL
-equivalent at INSERT
time. What would be the correct value for this purpose?
I don't know if it is you case, but if you are trying to add a column to an existing table, and then add a spatial index to that on a migration, the way I find to address this in MySQL was not an elegant solution, but worked:
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With