Search code examples
pythonpostgresqlsqlalchemysqlacodegen

SyntaxError: Invalid character in identifier when importing models generated using sqlacodegen


I am auto-generating tables in postgresql and models are autogenerated using sqlacodegen using names from a list.

Some of the names are non-ascii characters. For name 💞aussieBabe💞 which created a table 💞aussieBabe💞_index for which sqlAlchemy model is 💞aussieBabe💞Index. but when I try to import these models for querying I get below error.

line 147
    class 💞aussieBabe💞Index(Base):
                              ^
SyntaxError: invalid character in identifier

I assume that this can be solved by removing invalid characters before creating the table or restricting the class name. I want to restrict the class name which is generated by sqlacodegen, how can i achieve this? or can I set Unicode encoding for code?


Solution

  • Your table name contains some characters which not even Python 3 will allow. You need to fix the class name by hand. SQLACodegen cannot be expected to produce perfect results, as its documentation states too.