Skip to content

Commit 944d197

Browse files
authored
Merge pull request #561 from utopia-php/fix-create-index-key
Fix create index key not found
2 parents f87e860 + 6602b05 commit 944d197

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -762,26 +762,31 @@ public function createIndex(string $collection, string $id, string $type, array
762762
throw new NotFoundException('Collection not found');
763763
}
764764

765+
/**
766+
* We do not have internalId's added to list, since we check only for array field
767+
*/
765768
$collectionAttributes = \json_decode($collection->getAttribute('attributes', []), true);
766769

767770
$id = $this->filter($id);
768771

769772
foreach ($attributes as $i => $attr) {
770-
$collectionAttribute = \array_filter($collectionAttributes, fn ($collectionAttribute) => array_key_exists('key', $collectionAttribute) && $collectionAttribute['key'] === $attr);
771-
$collectionAttribute = end($collectionAttribute);
773+
$attribute = null;
774+
foreach ($collectionAttributes as $collectionAttribute) {
775+
if (\strtolower($collectionAttribute['$id']) === \strtolower($attr)) {
776+
$attribute = $collectionAttribute;
777+
break;
778+
}
779+
}
780+
772781
$order = empty($orders[$i]) || Database::INDEX_FULLTEXT === $type ? '' : $orders[$i];
773782
$length = empty($lengths[$i]) ? '' : '(' . (int)$lengths[$i] . ')';
774783

775-
$attr = match ($attr) {
776-
'$id' => '_uid',
777-
'$createdAt' => '_createdAt',
778-
'$updatedAt' => '_updatedAt',
779-
default => $this->filter($attr),
780-
};
784+
$attr = $this->getInternalKeyForAttribute($attr);
785+
$attr = $this->filter($attr);
781786

782787
$attributes[$i] = "`{$attr}`{$length} {$order}";
783788

784-
if (!empty($collectionAttribute['array']) && $this->getSupportForCastIndexArray()) {
789+
if ($this->getSupportForCastIndexArray() && !empty($attribute['array'])) {
785790
$attributes[$i] = '(CAST(`' . $attr . '` AS char(' . Database::ARRAY_INDEX_LENGTH . ') ARRAY))';
786791
}
787792
}

0 commit comments

Comments
 (0)