正确命令后使用 python aerich 出现错误

发布于 2025-01-09 07:04:44 字数 860 浏览 6 评论 0原文

我有 aerich 配置问题:

我有 app/database.py 文件,其中包含以下内容:

TORTOISE_ORM = {
    "connections": {"default": "postgres://..."},
    "apps": {
        "models": {
            "models": ["app.models", "aerich.models"],
            "default_connection": "default",
        },
    },
}

此外,我还有一个 python 包,其中包含位于 app/models/... 的乌龟表。。 我正在尝试使用以下命令创建第一个迁移文件:

  1. aerich init -t app.database.TORTOISE_ORM,并且我正在pyproject.toml内部获取数据:
[tool.aerich]
tortoise_orm = "app.database.TORTOISE_ORM"
location = "./migrations"
src_folder = "./."
  1. 之后我尝试运行aerich init-db,但收到错误:tortoise.exceptions.ConfigurationError:找不到模块“app.models” 我尝试了很多不同的变体来更改此配置,但得到了相同的错误。 aerich的版本==^0.6.2

大家知道这是怎么回事吗?

I have the problem with aerich configs:

I have the app/database.py file with the next content:

TORTOISE_ORM = {
    "connections": {"default": "postgres://..."},
    "apps": {
        "models": {
            "models": ["app.models", "aerich.models"],
            "default_connection": "default",
        },
    },
}

Also I have a python package with tortoise tables located in app/models/....
I'm trying to create first migrations file, using the next commands:

  1. aerich init -t app.database.TORTOISE_ORM and I'm getting data inside of my pyproject.toml:
[tool.aerich]
tortoise_orm = "app.database.TORTOISE_ORM"
location = "./migrations"
src_folder = "./."
  1. After it I'm trying to run aerich init-db, but I'm getting an error: tortoise.exceptions.ConfigurationError: Module "app.models" not found
    I tried a lot of different variants to change this configs but getting the same error. Version of aerich==^0.6.2

Does everybody know what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2025-01-16 07:04:44

字典中的 models 变量应包含包含数据库模型的模块。根据您的描述,您有一个包含模型文件的模型目录。例如,您有 hello.py,它在 models 文件夹中包含模型类,那么您需要提供“app.models.hello”而不仅仅是“app.models”。

The models variable in the dictionary should contain the module containing the Model for your database. As per your description, you have a models directory that contains files with the Model. For example, you have hello.py which contains model classes inside the models folder then you need to provide "app.models.hello" instead of just "app.models".

芸娘子的小脾气 2025-01-16 07:04:44

我看到您也在 aerich Github 存储库中提出了问题。在那里给出了答案,但为了完整起见,我将在这里重复一遍。

刚刚遇到这个 tortoise.exceptions.ConfigurationError: Module;我自己没有找到错误。使用调试器,我发现问题(至少在我的情况下)实际上并不是找不到模块。引发的 ImportError(导致 ConfigurationError)实际上暗示了循环导入的问题。奇怪的是,我在使用应用程序本身(运行相同的导入)时没有遇到这个循环导入问题,但在使用 aerich 时我确实遇到了这个问题...

长话短说:

  1. 解决循环导入可能会解决这个问题。
  2. Tortoise 应该选择不同的错误消息(例如,仅使用 ImportError 中的 msg),因为“找不到模块”可能根本不是实际问题。

I saw that you also asked this question in the aerich Github repo. Gave an answer there, but I'll repeat it here for completeness.

Just ran into this tortoise.exceptions.ConfigurationError: Module <x> not found error myself. Using a debugger, I found out that the problem - at least in my case - was actually not that the module could not be found. The ImportError raised (which leads to the ConfigurationError) was in fact hinting towards a problem with circular imports. Weirdly enough I don't get this circular import problem when using the app itself (which runs the same imports), but I do get it when using aerich...

Long story short:

  1. Solving circular imports might do the trick here.
  2. Tortoise should choose a different error message (e.g. just use the msg from the ImportError) as "Module not found" might simply not be the actual problem.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文