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

Composite unique index also marks a column unique=true #255

Closed
hubg398 opened this issue May 16, 2024 · 2 comments
Closed

Composite unique index also marks a column unique=true #255

hubg398 opened this issue May 16, 2024 · 2 comments
Assignees

Comments

@hubg398
Copy link

hubg398 commented May 16, 2024

Example DDL:

CREATE TABLE "participations"(
    "user_id" BIGINT NOT NULL,
    "project_id" BIGINT NOT NULL,
    "team_id" BIGINT NOT NULL,
);
ALTER TABLE
    "participations" ADD CONSTRAINT "participations_team_id_user_id_unique" UNIQUE("team_id", "user_id");

Output:


from simple_ddl_parser import DDLParser
import json

parse_results = DDLParser("""CREATE TABLE "participations"(
    "user_id" BIGINT NOT NULL,
    "project_id" BIGINT NOT NULL,
    "team_id" BIGINT NOT NULL,
);
ALTER TABLE
    "participations" ADD CONSTRAINT "participations_team_id_user_id_unique" UNIQUE("team_id", "user_id");""").run()

[
    {
        "table_name": "\"participations\"",
        "schema": null,
        "primary_key": [],
        "columns": [
            {
                "name": "\"user_id\"",
                "type": "BIGINT",
                "size": null,
                "references": null,
                "unique": true,                 ------- This should probably be false
                "nullable": false,
                "default": null,
                "check": null
            },
            {
                "name": "\"project_id\"",
                "type": "BIGINT",
                "size": null,
                "references": null,
                "unique": false,
                "nullable": false,
                "default": null,
                "check": null
            },
            {
                "name": "\"team_id\"",
                "type": "BIGINT",
                "size": null,
                "references": null,
                "unique": true,            ------- This should probably be false
                "nullable": false,
                "default": null,
                "check": null
            }
        ],
        "alter": {
            "uniques": [
                {
                    "constraint_name": "\"participations_team_id_user_id_unique\"",
                    "columns": [
                        "\"team_id\"",
                        "\"user_id\""
                    ]
                }
            ]
        },
        "checks": [],
        "index": [],
        "partitioned_by": [],
        "tablespace": null
    }
]

It correctly identifies the composite uniques in alter.uniques. But for the individual columns, they also show "unique: true". I believe that's incorrect since there would be no way to know if the column itself has a non-composite unique index.

Looks like it's been recently fixed here: #236. Maybe the ALTER TABLE ... ADD CONSTRAINT UNQIUE were missed out there?

@xnuinside
Copy link
Owner

@hubg398 I think your words totally make sense, need to fix it

@xnuinside
Copy link
Owner

fixed in 1.5.0, really appreciate for opening this issue, test added #256, if will be needed anything else - feel free to open new one

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

2 participants