2

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 aussieBabeIndex. but when I try to import these models for querying I get below error.

line 147
    class aussieBabeIndex(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?

Harwee
  • 1,601
  • 2
  • 21
  • 35
  • 1
    post a bug to the issue tracker! – Antti Haapala -- Слава Україні Oct 10 '17 at 18:56
  • 1
    If you can't wait for a bugfix, changing `_re_invalid_identifier` in https://github.com/agronholm/sqlacodegen/blob/master/sqlacodegen/codegen.py might be useful; or maybe even run it under Python2 to avoid unicode characters in identifiers altogether. – snakecharmerb Oct 10 '17 at 19:07
  • Will post bug report, for now I will follow @snakecharmerb suggestion to change `_re_invalid_identifier` – Harwee Oct 10 '17 at 19:52
  • @Harwee actually, are you sure you're running the same version? the regular expression `\W` *does match* `` so these should be removed... – Antti Haapala -- Слава Україні Oct 11 '17 at 10:24
  • Yes, those should be removed manually, but I have too many names with such problems, I need to create a mapping to dynamically point new classes, but anyway sqlacodegen should atleast raise an error instead of writing invalid chars which is handled by `_re_invalid_identifier` i guess. – Harwee Oct 11 '17 at 11:43

1 Answers1

1

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.

Alex Grönholm
  • 5,563
  • 29
  • 32