Skip to content

MySQL Unique Index syntax failing to parse #5479

@nathanchapman

Description

@nathanchapman

Dialect: mysql
Confirmed issue on latest (27.3.1)

anonymous UNIQUE parses ✅

sql = """
        CREATE TABLE `abc_def` (
            `id` bigint NOT NULL AUTO_INCREMENT,
            `name` varchar(100) NOT NULL,
            `uuid` varchar(100) NOT NULL,
            `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `date_updated` datetime NULL ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            UNIQUE (`name`),
            UNIQUE (`uuid`)
        ) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
"""

named UNIQUE KEY parses ✅

sql = """
        CREATE TABLE `abc_def` (
            `id` bigint NOT NULL AUTO_INCREMENT,
            `name` varchar(100) NOT NULL,
            `uuid` varchar(100) NOT NULL,
            `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `date_updated` datetime NULL ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            UNIQUE KEY `name_idx` (`name`),
            UNIQUE KEY `uuid_idx` (`uuid`)
        ) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
"""

named UNIQUE INDEX fails parsing ❌

sql = """
        CREATE TABLE `abc_def` (
            `id` bigint NOT NULL AUTO_INCREMENT,
            `name` varchar(100) NOT NULL,
            `uuid` varchar(100) NOT NULL,
            `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `date_updated` datetime NULL ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            UNIQUE INDEX `name_idx` (`name`),
            UNIQUE INDEX `uuid_idx` (`uuid`)
        ) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
"""

Error:

        if self.error_level == ErrorLevel.IMMEDIATE:
>           raise error
E           sqlglot.errors.ParseError: Expecting ). Line 9, Col: 35.
E             datetime NULL ON UPDATE CURRENT_TIMESTAMP,
E                       PRIMARY KEY (`id`),
E                       UNIQUE INDEX `name_idx` (`name`),
E                       UNIQUE INDEX `uuid_idx` (`uuid`)
E                   ) CHARSET utf8mb4 COLLATE utf8mb4_09

EDIT:

In the meantime, I'm using this to get around the issue:

    if dialect is Dialects.MYSQL:
        sql = sql.replace("UNIQUE INDEX", "UNIQUE KEY")

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions