Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aerich upgrade 'Failed to open the referenced table' problem in one-to-many #354

Open
yuWorm opened this issue Aug 6, 2024 · 1 comment

Comments

@yuWorm
Copy link

yuWorm commented Aug 6, 2024

When I use a one-to-many association, the SQL statement generated by calling 'aerich migrate' does not sort according to the association information, and an exception 'Failed to open the referenced table' will appear. Is it possible to add sorting based on the association information?
image

@yuWorm
Copy link
Author

yuWorm commented Aug 6, 2024

I solved the problem by adding sorting, here is the relevant code

import graphlib
from aerich.migrate import Migrate
from aerich import utils


def get_models_describe_by_order(app: str) -> dict:
    ret = utils.get_models_describe(app=app)
    result = {}
    graph = {}
    for k, v in ret.items():
        graph[k] = []
        fk_fields = v.get("fk_fields", "")
        if len(fk_fields) == 0:
            continue
        for fk_field in fk_fields:
            python_type = fk_field.get("python_type")
            if python_type:
                graph[k].append(python_type)
    ts = graphlib.TopologicalSorter(graph)
    order_res = tuple(ts.static_order())
    for name in order_res:
        result[name] = ret[name]
    return result


async def migrate(cls, name) -> str:
    new_version_content = get_models_describe_by_order(cls.app)
    cls.diff_models(cls._last_version_content, new_version_content)
    cls.diff_models(new_version_content, cls._last_version_content, False)

    cls._merge_operators()

    if not cls.upgrade_operators:
        return ""

    return await cls._generate_diff_py(name)


Migrate.migrate = classmethod(migrate)


def get_cli():
    from aerich.cli import cli

    return cli


cli = get_cli()

__all__ = ["cli"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant