tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
830 stars 95 forks source link

KeyError: 'UTF8_UNICODE_CI' on processing a text column with COLLATE statement #208

Open evgenybf opened 2 years ago

evgenybf commented 2 years ago

tortoise-orm[asyncmy]==0.17.8 asyncmy==0.2.3 aerich==0.5.8

aerich inspectdb fails with

Traceback (most recent call last):
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/bin/aerich", line 8, in <module>
    sys.exit(main())
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/aerich/cli.py", line 258, in main
    cli()
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/aerich/cli.py", line 33, in wrapper
    loop.run_until_complete(f(*args, **kwargs))
  File "/home/efilatov/.pyenv/versions/3.8.11/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/aerich/cli.py", line 254, in inspectdb
    await command.inspectdb(table)
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/aerich/__init__.py", line 112, in inspectdb
    await inspect.inspect()
  File "/home/efilatov/.local/share/virtualenvs/test-aerich-iRKKyBaN/lib/python3.8/site-packages/aerich/inspectdb.py", line 79, in inspect
    field = self._field_template_mapping[column.data_type].format(
KeyError: 'UTF8_UNICODE_CI'

It's caused by this table:

CREATE TABLE `UI_LANGUAGE` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `LANGUAGE` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `STRING` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Aerich iterates through the following column_name, column.data_type list:

  1. id INT
  2. language TEXT
  3. collate UTF8_UNICODE_CI <- this one causes the error