I have a db table with a JSON column. I now want to add an index to parts of that json.
It turns out you can only add an index on a json column when creating the table.
Here's what I tried in a migration:
DB::statement(DB::raw(<<<SQL
CREATE TABLE area_groups (
title JSON,
`created_at` timestamp null,
`updated_at` timestamp null,
INDEX area_groups_title_de (
(
JSON_VALUE(title, '$.de')
)
),
INDEX area_groups_title_en (
(
JSON_VALUE(title, '$.en')
)
)
) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
SQL
));
Schema::table('area_groups', function (Blueprint $table) {
$table->id()->change();
$table->foreignId('area_id')->change()->constrained();
});
My idea was to create the json column and indexes in a raw db statement and do the rest with Laravel's migration helpers.
Creating the table seems to work, but running this migration fails with the following error message:
Argument 1 passed to Doctrine\DBAL\Schema\Index::_addColumn() must be of the type string, null given, called in vendor/doctrine/dbal/src/Schema/Index.php on line 72
create index area_groups_title_de on area_groups(JSON_VALUE(title, '$.de'));gives me a mysql syntax error.create tablestatement manually on a console it works.