Skip to content

Invalid name generation when using python reserved keywords #88

@QuentinN42

Description

@QuentinN42

Given the following SQL schema:

CREATE TABLE authors (
          id    BIGSERIAL PRIMARY KEY,
          class text      NOT NULL
);

sqlc will generate the following python code :

class Author(pydantic.BaseModel):
    id: int
    class: str

This is not a valid python code :

>>> import pydantic
... 
... 
... class Author(pydantic.BaseModel):
...     id: int
...     class: str
... 
  File "<python-input-0>", line 6
    class: str
         ^
SyntaxError: invalid syntax

This is due to class to be a reserved python keyword.
Note that it will also fails if you use if, not, in, etc...

I have generated an example on my fork: https://github.com/Escape-Technologies/sqlc-gen-python/tree/19e8d56813a83664bc19c12a3121e84e8bc7af66/internal/endtoend/testdata/emit_pydantic_models_with_fields

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions