andialbrecht / sqlparse

A non-validating SQL parser module for Python
BSD 3-Clause "New" or "Revised" License
3.63k stars 685 forks source link

Keyword `PRIMARY KEY` is not parsed correctly #740

Closed ozooxo closed 3 months ago

ozooxo commented 9 months ago

Describe the bug Keyword PRIMARY KEY is not parsed correctly. Instead, KEY, <next identifier> is parsed into a nested TokenList.

To Reproduce It can be reproduced by executing the following python script:

import sqlparse

statements = sqlparse.parse("""
CREATE TABLE foo (
    id serial PRIMARY KEY,
    data text
)
""")

statements[0].tokens[7]

statements[0].tokens[7].tokens

statements[0].tokens[7].tokens[12]

statements[0].tokens[7].tokens[12].tokens

Screenshot 2023-09-27 at 11 26 05 PM

Expected behavior [<Keyword 'KEY' at 0x104DC88E0>, <Punctuation ',' at 0x104DC8940>, <Newline ' ' at 0x104DC89A0>, <Whitespace ' ' at 0x104DC8A00>, <Keyword 'data' at 0x104DC8A60>] should be flattened into the Parenthesis array, instead of some parts before , and some parts after it become a IdentifierList.

Versions (please complete the following information):

Additional context N/A

keredson commented 4 months ago

i also hit this issue.

andialbrecht commented 3 months ago

Thanks for reporting!