Repository: macbre/sql-metadata

Issue:
Wrong `tables` in parsed result with sql like `ALTER TABLE ... ADD KEY ...`
Different results of `Parser(sql)` in these sqls:

- `ADD KEY`: columns are considered as tables, 2 results in `tables`
- `ADD INDEX`: 1 table matched

<img width="835" alt="Image" src="https://github.com/user-attachments/assets/dd2d4227-e89e-46ec-bb25-96f7919ce8e0" />

Discussion:

Fix (patch):
diff --git a/sql_metadata/keywords_lists.py b/sql_metadata/keywords_lists.py
index 4abeaea..53f3334 100644
--- a/sql_metadata/keywords_lists.py
+++ b/sql_metadata/keywords_lists.py
@@ -125,6 +125,7 @@ RELEVANT_KEYWORDS = {
     "RETURNING",
     "VALUES",
     "INDEX",
+    "KEY",
     "WITH",
     "WINDOW",
 }


Test changes:
diff --git a/test/test_alter.py b/test/test_alter.py
new file mode 100644
index 0000000..572dba2
--- /dev/null
+++ b/test/test_alter.py
@@ -0,0 +1,13 @@
+from sql_metadata import Parser, QueryType
+
+
+def test_alter_table_indices_key():
+    parser = Parser("ALTER TABLE foo_table ADD KEY `idx_foo` (`bar`);")
+    assert parser.query_type == QueryType.ALTER
+    assert parser.tables == ["foo_table"]
+
+
+def test_alter_table_indices_index():
+    parser = Parser("ALTER TABLE foo_table ADD INDEX `idx_foo` (`bar`);")
+    assert parser.query_type == QueryType.ALTER
+    assert parser.tables == ["foo_table"]