Skip to content

sql_db missing index with foreign key from multiple field primary key #634

Description

@moellep

A meta definition like this:

            plot=PKDict(
                plot_id="primary_id 6",
                material_id="primary_id",
                title="str 100",
                xlabel="str 100",
                ylabel="str 100",
                type="str 10",
            ),
            plot_value=PKDict(
                plot_id="primary_id primary_key",
                dim="int 32 primary_key",  # x|y1|y2|y3
                idx="int 32 primary_key",
                value="float 64",
            ),

creates the follow sql schma:

CREATE TABLE plot (
        plot_id BIGINT NOT NULL,
        material_id BIGINT NOT NULL,
        title VARCHAR(100) NOT NULL,
        xlabel VARCHAR(100) NOT NULL,
        ylabel VARCHAR(100) NOT NULL,
        type VARCHAR(10) NOT NULL,
        PRIMARY KEY (plot_id),
        FOREIGN KEY(material_id) REFERENCES material (material_id)
);
CREATE INDEX ix_plot_material_id ON plot (material_id);
CREATE TABLE plot_value (
        plot_id BIGINT NOT NULL,
        dim INTEGER NOT NULL,
        idx INTEGER NOT NULL,
        value FLOAT NOT NULL,
        PRIMARY KEY (plot_id, dim, idx),
        FOREIGN KEY(plot_id) REFERENCES plot (plot_id)
);

But it is missing the index on plot_value.plot_id which seems to be created if the table has the foreign key outside of the primary key.
Adding the additional "index" keyword works around the problem for now:

            plot_value=PKDict(
                plot_id="primary_id primary_key index",
                dim="int 32 primary_key",  # x|y1|y2|y3
                idx="int 32 primary_key",
                value="float 64",
            ),

which creates the expected sql schema:

CREATE TABLE plot_value (
        plot_id BIGINT NOT NULL,
        dim INTEGER NOT NULL,
        idx INTEGER NOT NULL,
        value FLOAT NOT NULL,
        PRIMARY KEY (plot_id, dim, idx),
        FOREIGN KEY(plot_id) REFERENCES plot (plot_id)
);
CREATE INDEX ix_plot_value_plot_id ON plot_value (plot_id);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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