Repository: tobymao/sqlglot

Issue:
MySQL Named Primary Key fails to parse
**Summary**

MySQL will parse a CREATE TABLE statement which specifies a name for a PRIMARY KEY (it will ignore that name and call it PRIMARY but it should parse). sqlglot fails with a parse error in this case, even if mysql dialect is specified

**Repro**

```
import sqlglot
query = """
create table test
(
	id int,
    value varchar(5),
    primary key `test_pk` (id)
);
"""
sqlglot.parse_one(query, read="mysql")
```

**Expected**

SQL should parse successfully. Primary Key name should be ignored.

**Actual**

sqlglot.errors.ParseError: Expecting ). Line 6, Col: 25.

**Environment**

sqlglot: 28.0.0
python: 3.10.5
OS: Ubuntu running under WSL for Windows 11

Discussion:

Fix (patch):
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 18d2dcb427..999c414697 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -6512,6 +6512,13 @@ def _parse_primary_key(
             and self._prev.token_type == TokenType.DESC
         )
 
+        if (
+            self._curr.text.upper() not in self.CONSTRAINT_PARSERS
+            and self._next
+            and self._next.token_type == TokenType.L_PAREN
+        ):
+            self._parse_id_var()
+
         if not in_props and not self._match(TokenType.L_PAREN, advance=False):
             return self.expression(
                 exp.PrimaryKeyColumnConstraint,


Test changes:
diff --git a/tests/dialects/test_mysql.py b/tests/dialects/test_mysql.py
index 465ff0fb88..8a65bc9e3e 100644
--- a/tests/dialects/test_mysql.py
+++ b/tests/dialects/test_mysql.py
@@ -95,6 +95,19 @@ def test_ddl(self):
         self.validate_identity(
             "CREATE TABLE test_table (id INT AUTO_INCREMENT, PRIMARY KEY (id) USING HASH)"
         )
+        self.validate_identity(
+            "CREATE TABLE test (id INT, PRIMARY KEY pk_name (id))",
+            "CREATE TABLE test (id INT, PRIMARY KEY (id))",
+        )
+        self.validate_identity(
+            "CREATE TABLE test (id INT, PRIMARY KEY `pk_name` (id))",
+            "CREATE TABLE test (id INT, PRIMARY KEY (id))",
+        )
+        self.validate_identity(
+            'CREATE TABLE test (id INT, PRIMARY KEY "pk_name" (id))',
+            "CREATE TABLE test (id INT, PRIMARY KEY (id))",
+        )
+        self.validate_identity("CREATE TABLE test (id INT, CONSTRAINT pk_name PRIMARY KEY (id))")
         self.validate_identity(
             "CREATE TABLE test (a INT, b INT GENERATED ALWAYS AS (a + a) STORED)"
         )