tortoise / aerich

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

出现Error: No config named "xxx"是版本不兼容问题吗? #164

Closed h25302 closed 3 years ago

h25302 commented 3 years ago

环境: python:3.9.5 tortoise-orm:0.17.3 aerich:0.5.3 OS:windowns10

问题描述: 运行aerich init -t xxx.TORTOISE_ORM后,能够正常生成aerich.ini文件和migrations目录 但是,接下来运行 aerich init-db时,出现提示Error: No config named "xxx"

网上搜了一圈,好像也没什么人碰到这个问题,只看到有1个链接是一样的问题,但是没有解决: https://ask.csdn.net/questions/3985644 希望老大能在百忙之中,能否帮忙看看是什么地方出现问题了呀

long2ice commented 3 years ago

找不到配置路径吧,检查一下aerich.ini

h25302 commented 3 years ago

ini文件看起来很正常的啊:

[aerich] tortoise_orm = xxx.TORTOISE_ORM location = ./migrations

h25302 commented 3 years ago

https://github.com/tortoise/aerich/issues/74 https://github.com/tortoise/aerich/issues/95

原来是早就有人提问过类似问题的,但感觉都是没解决呀。 老大是不能重现此问题吗? 要是在哪里能够搭个环境让老大测试一下就好了

long2ice commented 3 years ago

原理是执行命令的时候会把当前路径加入PYTHONPATH然后去寻找配置, 你看看你的配置文件里面的与实际的项目结构能不能对应上

h25302 commented 3 years ago

目录结构如下: E:\project ├─migrations ├─models │ └─user.py │ ├─aerich.ini └─main.py

aerich.ini的内容如下: [aerich] tortoise_orm = main.TORTOISE_ORM location = ./migrations

main.py的内容如下: TORTOISE_ORM = { 'connections': {'default': mysql://root:123456@localhost/test}, 'apps': { 'models': { 'models': ['aerich.models', 'models.user'], 'default_connection': 'default' } } } 运行aerich的时候,是在E:\project中运行:(.python) PS E:\project> aerich init-db 运行后提示:Error: No config named "main"

long2ice commented 3 years ago

不确定是不是Windows下不生效

h25302 commented 3 years ago

能否提供一个方法,不需要运行aerich.exe 而是直接通过写代码,去执行和管理Migration的过程

long2ice commented 3 years ago

暂时不支持

h25302 commented 3 years ago

有在windows下,进行测试和解决的计划吗?

long2ice commented 3 years ago

欢迎PR,我平时不用Windows开发

h25302 commented 3 years ago

不能写代码去执行 init-db的过程,都没法调试啊。能否指点一下用什么方法调试? 我去试试能否解决这个问题

long2ice commented 3 years ago

init-db 实际上也是执行的cli脚本,等价于python -m aerich.cli init-db,你可以看看源代码

h25302 commented 3 years ago

多谢多谢,问题已经解决,并非是windows环境问题

总结如下,后来者可以借鉴一下: aerich是通过importlib.import_module来加载TORTOISE_ORM配置的: try: config_module = importlib.import_module(config_path) except (ModuleNotFoundError, AttributeError): raise BadOptionUsage( ctx=ctx, message=f'No config named "{config_path}"', option_name="--config" )

所以在import_module加载过程中,出现的任何ModuleNotFoundError和AttributeError,都只会出现提示No config name,这有可能会掩盖了真正的错误,导致问题无法解决。

具体到我自己的问题,是由于没有安装aiofiles模块,而在import_module加载配置的过程中,有些代码用到了aiofiles模块,恰好也出现了ModuleNotFoundError,但是提示的却是No config name,导致解决该问题找错了方向。 另外,一定要注意一下,在TORTOISE_ORM配置文件中,是否存在一些自动执行的代码,会不会导致出现ModuleNotFoundError和AttributeError

最后pip install aiofiles后,问题解决,现在aerich init-db可以正常执行了

最后提示: 经过我自己测试,在安装FastApi的时候, 用pip install fastapi[all] 安装aiofiles, 而用pip install fastapi 不会安装aiofiles, 不幸的是,我恰好用的是pip install fastapi,所以这是出现该问题的根本原因,希望大伙注意一下。

long2ice commented 3 years ago

感谢分享!