tortoise / aerich

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

M2M table has been duplicated and cant migrate #201

Open ehdgua01 opened 3 years ago

ehdgua01 commented 3 years ago

Problem

Two tables have created when I add a new M2M field that through my custom table

Before.

class User(models.Model):
    name = fields.CharField(max_length=10)
-- upgrade --
CREATE TABLE IF NOT EXISTS `user` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(10) NOT NULL
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `aerich` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `version` VARCHAR(255) NOT NULL,
    `app` VARCHAR(20) NOT NULL,
    `content` JSON NOT NULL
) CHARACTER SET utf8mb4;

After.

class User(models.Model):
    name = fields.CharField(max_length=10)
    user_groups = fields.ManyToManyField("models.UserGroup", through="user_user_group_relation")

    class Meta:
        table = "user"

class UserGroup(models.Model):
    name = fields.CharField(max_length=10)

    class Meta:
        table = "user_group"

class UserUserGroupRelation(models.Model):
    user = fields.ForeignKeyField("models.User")
    user_group = fields.ForeignKeyField("models.UserGroup")
    is_active = fields.BooleanField()

    class Meta:
        table = "user_user_group_relation"

user_user_group_relation table duplicated

-- upgrade --
CREATE TABLE IF NOT EXISTS `user_group` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(10) NOT NULL
) CHARACTER SET utf8mb4;;
CREATE TABLE IF NOT EXISTS `user_user_group_relation` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `is_active` BOOL NOT NULL,
    `user_id` INT NOT NULL,
    `user_group_id` INT NOT NULL,
    CONSTRAINT `fk_user_use_user_9a3f6327` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_user_use_user_gro_1ced338a` FOREIGN KEY (`user_group_id`) REFERENCES `user_group` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;;
CREATE TABLE `user_user_group_relation` (`user_id` INT NOT NULL REFERENCES `user` (`id`) ON DELETE CASCADE,`usergroup_id` INT NOT NULL REFERENCES `user_group` (`id`) ON DELETE CASCADE) CHARACTER SET utf8mb4;
-- downgrade --
DROP TABLE IF EXISTS `user_user_group_relation`;
DROP TABLE IF EXISTS `user_group`;
ehdgua01 commented 3 years ago

Duplicated: https://github.com/tortoise/aerich/issues/150

Error raised after I removed duplicated M2M table(user_user_group_relation) I applied this SQL

-- upgrade --
CREATE TABLE IF NOT EXISTS `user_group` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(10) NOT NULL
) CHARACTER SET utf8mb4;;
CREATE TABLE IF NOT EXISTS `user_user_group_relation` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `is_active` BOOL NOT NULL,
    `user_id` INT NOT NULL,
    `user_group_id` INT NOT NULL,
    CONSTRAINT `fk_user_use_user_9a3f6327` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_user_use_user_gro_1ced338a` FOREIGN KEY (`user_group_id`) REFERENCES `user_group` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;;
-- downgrade --
DROP TABLE IF EXISTS `user_user_group_relation`;
DROP TABLE IF EXISTS `user_group`;

And I add a new table with a new M2M field

class UserGroup(models.Model):
    name = fields.CharField(max_length=10)
    # new M2M field
    operation_set = fields.ManyToManyField("models.Operation", related_name="usergroup_set")

    class Meta:
        table = "user_group"

# New table
class Operation(orm.AbstractBaseModel):
    name = fields.CharField(max_length=255)
    operation_id = fields.CharField(max_length=255)
    description = fields.TextField()

    class Meta:
        table = "operation"

And I ran this command

poetry run aerich migrate

Finally, I got this error... 😢

Traceback (most recent call last):
  File "/Users/A202006042/.virtualenvs/myproj/bin/aerich", line 8, in <module>
    sys.exit(main())
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/aerich/cli.py", line 258, in main
    cli()
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/click/core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/aerich/cli.py", line 33, in wrapper
    loop.run_until_complete(f(*args, **kwargs))
  File "/Users/A202006042/.pyenv/versions/3.9.4/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/aerich/cli.py", line 91, in migrate
    ret = await command.migrate(name)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/aerich/__init__.py", line 115, in migrate
    return await Migrate.migrate(name)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/aerich/migrate.py", line 130, in migrate
    cls.diff_models(cls._last_version_content, new_version_content)
  File "/Users/A202006042/.virtualenvs/myproj/lib/python3.9/site-packages/aerich/migrate.py", line 214, in diff_models
    table = change[0][1].get("through")
AttributeError: 'str' object has no attribute 'get'
ehdgua01 commented 3 years ago

@long2ice, Please help me 🙏🏿

zy1000 commented 4 months ago

我最近也遇到了这个问题,请问你解决了吗