diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp
--- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp
+++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp
@@ -1,6 +1,7 @@
 #include "SQLiteQueryExecutor.h"
 #include "Logger.h"
 
+#include "../NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageTypeEnum.h"
 #include "entities/CommunityInfo.h"
 #include "entities/EntityQueryHelpers.h"
 #include "entities/EntryInfo.h"
@@ -211,6 +212,48 @@
   return false;
 }
 
+bool update_messages_idx_target_message_type_time(sqlite3 *db) {
+  char *error;
+  int sidebarSourceTypeInt = static_cast<int>(MessageType::SIDEBAR_SOURCE);
+  std::string sidebarSourceType = std::to_string(sidebarSourceTypeInt);
+
+  auto query =
+      "DROP INDEX IF EXISTS messages_idx_target_message_type_time;"
+      "ALTER TABLE messages DROP COLUMN target_message;"
+      "ALTER TABLE messages "
+      "  ADD COLUMN target_message TEXT "
+      "  AS (IIF("
+      "    JSON_VALID(content),"
+      "    COALESCE("
+      "      JSON_EXTRACT(content, '$.targetMessageID'),"
+      "      IIF("
+      "        type = " +
+      sidebarSourceType +
+      "        , JSON_EXTRACT(content, '$.id'),"
+      "        NULL"
+      "      )"
+      "    ),"
+      "    NULL"
+      "  ));"
+      "CREATE INDEX IF NOT EXISTS messages_idx_target_message_type_time "
+      "  ON messages (target_message, type, time);";
+
+  sqlite3_exec(db, query.c_str(), nullptr, nullptr, &error);
+
+  if (!error) {
+    return true;
+  }
+
+  std::ostringstream stringStream;
+  stringStream
+      << "Error creating (target_message, type, time) index on messages table: "
+      << error;
+  Logger::log(stringStream.str());
+
+  sqlite3_free(error);
+  return false;
+}
+
 bool create_media_table(sqlite3 *db) {
   std::string query =
       "CREATE TABLE IF NOT EXISTS media ( "
@@ -747,8 +790,9 @@
 
 bool create_schema(sqlite3 *db) {
   char *error;
-  sqlite3_exec(
-      db,
+  int sidebarSourceTypeInt = static_cast<int>(MessageType::SIDEBAR_SOURCE);
+  std::string sidebarSourceType = std::to_string(sidebarSourceTypeInt);
+  auto query =
       "CREATE TABLE IF NOT EXISTS drafts ("
       "  key TEXT UNIQUE PRIMARY KEY NOT NULL,"
       "  text TEXT NOT NULL"
@@ -766,7 +810,15 @@
       "  target_message TEXT AS ("
       "    IIF("
       "      JSON_VALID(content),"
-      "      JSON_EXTRACT(content, '$.targetMessageID'),"
+      "      COALESCE("
+      "        JSON_EXTRACT(content, '$.targetMessageID'),"
+      "        IIF("
+      "          type = " +
+      sidebarSourceType +
+      "          , JSON_EXTRACT(content, '$.id'),"
+      "          NULL"
+      "        )"
+      "      ),"
       "      NULL"
       "    )"
       "  )"
@@ -918,11 +970,9 @@
       "  ON messages (target_message, type, time);"
 
       "CREATE INDEX IF NOT EXISTS outbound_p2p_messages_idx_id_timestamp"
-      "  ON outbound_p2p_messages (device_id, timestamp);",
+      "  ON outbound_p2p_messages (device_id, timestamp);";
 
-      nullptr,
-      nullptr,
-      &error);
+  sqlite3_exec(db, query.c_str(), nullptr, nullptr, &error);
 
   if (!error) {
     return true;
@@ -1170,7 +1220,8 @@
      {47, {create_message_store_local_table, true}},
      {48, {create_messages_idx_target_message_type_time, true}},
      {49, {add_supports_auto_retry_column_to_p2p_messages_table, true}},
-     {50, {create_message_search_table, true}}}};
+     {50, {create_message_search_table, true}},
+     {51, {update_messages_idx_target_message_type_time, true}}}};
 
 enum class MigrationResult { SUCCESS, FAILURE, NOT_APPLIED };
 
@@ -2580,9 +2631,9 @@
       searchMessagesSQL.str(),
       "Failed to get message search results");
 
-  auto SIDEBAR_SOURCE_TYPE = 17;
+  auto sidebarSourceType = static_cast<int>(MessageType::SIDEBAR_SOURCE);
 
-  bindIntToSQL(SIDEBAR_SOURCE_TYPE, preparedSQL, 1);
+  bindIntToSQL(sidebarSourceType, preparedSQL, 1);
   bindStringToSQL(threadID.c_str(), preparedSQL, 2);
   bindStringToSQL(query.c_str(), preparedSQL, 3);
   bindStringToSQL(threadID.c_str(), preparedSQL, 4);
diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs.h
--- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs.h
+++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs.h
@@ -17,37 +17,12 @@
 #include "MessageSpecs/TextMessageSpec.h"
 #include "MessageSpecs/UnsupportedMessageSpec.h"
 #include "MessageSpecs/UpdateRelationshipMessageSpec.h"
+#include "MessageTypeEnum.h"
 
 #include <map>
 
 namespace comm {
 
-enum class MessageType {
-  TEXT,
-  CREATE_THREAD,
-  ADD_MEMBERS,
-  CREATE_SUB_THREAD,
-  CHANGE_SETTINGS,
-  REMOVE_MEMBERS,
-  CHANGE_ROLE,
-  LEAVE_THREAD,
-  JOIN_THREAD,
-  CREATE_ENTRY,
-  EDIT_ENTRY,
-  DELETE_ENTRY,
-  RESTORE_ENTRY,
-  UNSUPPORTED,
-  IMAGES,
-  MULTIMEDIA,
-  LEGACY_UPDATE_RELATIONSHIP,
-  SIDEBAR_SOURCE,
-  CREATE_SIDEBAR,
-  REACTION,
-  EDIT_MESSAGE,
-  TOGGLE_PIN,
-  UPDATE_RELATIONSHIP,
-};
-
 const std::map<MessageType, std::unique_ptr<MessageSpec>> messageSpecsHolder =
     []() {
       std::map<MessageType, std::unique_ptr<MessageSpec>>
diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageTypeEnum.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageTypeEnum.h
new file mode 100644
--- /dev/null
+++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageTypeEnum.h
@@ -0,0 +1,28 @@
+
+namespace comm {
+enum class MessageType {
+  TEXT = 0,
+  CREATE_THREAD = 1,
+  ADD_MEMBERS = 2,
+  CREATE_SUB_THREAD = 3,
+  CHANGE_SETTINGS = 4,
+  REMOVE_MEMBERS = 5,
+  CHANGE_ROLE = 6,
+  LEAVE_THREAD = 7,
+  JOIN_THREAD = 8,
+  CREATE_ENTRY = 9,
+  EDIT_ENTRY = 10,
+  DELETE_ENTRY = 11,
+  RESTORE_ENTRY = 12,
+  UNSUPPORTED = 13,
+  IMAGES = 14,
+  MULTIMEDIA = 15,
+  LEGACY_UPDATE_RELATIONSHIP = 16,
+  SIDEBAR_SOURCE = 17,
+  CREATE_SIDEBAR = 18,
+  REACTION = 19,
+  EDIT_MESSAGE = 20,
+  TOGGLE_PIN = 21,
+  UPDATE_RELATIONSHIP = 22,
+};
+} // namespace comm
diff --git a/web/shared-worker/_generated/comm_query_executor.wasm b/web/shared-worker/_generated/comm_query_executor.wasm
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
GIT binary patch
literal 0
Hc$@<O00001

literal 0
Hc$@<O00001