aerich icon indicating copy to clipboard operation
aerich copied to clipboard

[BUG] Sort m2m fields before comparing them with `diff(...)`

Open mykolasolodukha opened this issue 3 years ago • 1 comments

What?

  • [x] Sort new_m2m_fields before comparing them using dictdiffer.diff(...).

Why?

Otherwise, if one adds more m2m fields, the diff would produce the action="change" result, instead of the action="add" one. See:

  • https://github.com/inveniosoftware/dictdiffer/issues/100
  • https://github.com/inveniosoftware/dictdiffer/issues/170

How to test?

Try migrating this:

# models.py
class User(tortoise.Model):

    id = fields.BigIntField(pk=True, generated=False)

    groups: fields.ManyToManyRelation[Group]

class Group(tortoise.Model):

    users: fields.ManyToManyRelation[User] = fields.ManyToManyField(
        "bot.User", through="group__user", related_name="groups"
    )

To this:

# models.py
class User(tortoise.Model):

    id = fields.BigIntField(pk=True, generated=False)

    groups: fields.ManyToManyRelation[Group]
    admin_in_groups: fields.ManyToManyRelation[Group]

class Group(tortoise.Model):

    users: fields.ManyToManyRelation[User] = fields.ManyToManyField(
        "bot.User", through="group__user", related_name="groups"
    )
    admins: fields.ManyToManyRelation[User] = fields.ManyToManyField(
        "bot.User", through="group__admin", related_name="admin_in_groups"
    )
aerich migrate --name "add_group_admins"

Anything else?

I think this kind of sorting might help with other types of fields as well, but I have no time to investigate that right now. The fix in this PR is urgent to me, though.

mykolasolodukha avatar Oct 13 '22 16:10 mykolasolodukha

I have the same problem, aerich doesn't work at all. This PR solves the problem. Please merge this!

LanceMoe avatar Oct 25 '22 03:10 LanceMoe