Skip to content

tortoise makemigration generated duplicate table #2193

@miss85246

Description

@miss85246

Describe the bug
makemigration will generate duplicate table:

class Role(BaseApplicationModel):
    name = CharField(max_length=16, description="角色名称")
    description = CharField(max_length=128, default="", blank=True, description="角色描述")
    snap = JSONField(default=dict, description="角色权限快照【{resource:code}】")
    reports = JSONField(default=[], description="用户报表权限")

    permissions = ManyToManyField("models.Permission", related_name="roles", through="auth_role_perms")

    class Meta:
        table = "auth_role"
        table_description = "角色表"

class Permission(BaseModel):
    name = CharField(max_length=64, db_index=True, description="权限名称")
    resource = CharField(max_length=64, unique=True, db_index=True, description="权限资源")
    description = CharField(max_length=128, blank=True, default="", description="权限描述")
    level = IntField(default=0, description="应用等级(大于等于该等级的应用才会展示该权限)")
    allow = JSONField(default=list(allow_opt.keys()), null=True, description="支持操作")

    class Meta:
        table = "auth_permission"
        table_description = "权限表"
        ordering = []


class RolePermission(BaseModel):
    role = ForeignKey("models.Role", related_name="ref_permission", description="角色")
    permission = ForeignKey("models.Permission", related_name="ref_role", to_field="resource", description="权限")
    role_name = CharField(max_length=32, default="", blank=True, description="角色名称【冗余显示用】")
    permission_name = CharField(max_length=32, default="", blank=True, description="权限名称【冗余显示用】")
    mask = IntField(default=0, description="权限掩码")

    class Meta:
        table = "auth_role_perms"
        table_description = "角色权限表"
        unique_together = [("role", "permission")]

there after execute command

tortoise makemigration
tortoise migratesql

and output the sql :

CREATE TABLE "auth_role_perms" (
    "auth_role_id" BIGINT NOT NULL REFERENCES "auth_role" ("id") ON DELETE CASCADE,
    "permission_id" BIGINT NOT NULL REFERENCES "auth_permission" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX "uidx_auth_role_p_auth_ro_ade6c0" ON "auth_role_perms" ("auth_role_id", "permission_id");


CREATE TABLE "auth_role_perms" (
    "id" BIGSERIAL NOT NULL PRIMARY KEY,
    "deleted" BOOL NOT NULL,
    "utime" TIMESTAMPTZ,
    "ctime" TIMESTAMPTZ,
    "system" BOOL NOT NULL,
    "comment" VARCHAR(255),
    "role_name" VARCHAR(32) NOT NULL,
    "permission_name" VARCHAR(32) NOT NULL,
    "mask" INT NOT NULL,
    "permission_id" VARCHAR(64) NOT NULL REFERENCES "auth_permission" ("resource") ON DELETE CASCADE,
    "role_id" BIGINT NOT NULL REFERENCES "auth_role" ("id") ON DELETE CASCADE,
    CONSTRAINT "uid_auth_role_p_role_id_9994fe" UNIQUE ("role_id", "permission_id")
);

The SQL script generated duplicate CREATE TABLE statements for the same table.

To Reproduce
Steps to reproduce the behavior, preferably a small code snippet.

Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions