tortoise / tortoise/tortoise-orm

tortoise makemigration generated duplicate table

Open
#2,193 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the Role, Permission, and RolePermission models, then run tortoise makemigration followed by tortoise migratesql. Trace how the explicit auth_role_perms through table and the RolePermission model are processed. Done means the generated SQL contains one table definition while retaining the intended columns and uniqueness constraint.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.