Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Unique constraint on a column in sqlite database

I am trying to remove a UNIQUE constraint on a column for sqlite but I do not have the name to remove the constraint. How can I find the name of the UNIQUE constraint name to remove it.

Below is the schema I see for the table I want to remove the constraint

UNIQUE (datasource_name)

sqlite> .schema datasources
CREATE TABLE "datasources" (
    created_on DATETIME NOT NULL, 
    changed_on DATETIME NOT NULL, 
    id INTEGER NOT NULL, 
    datasource_name VARCHAR(255), 
    is_featured BOOLEAN, 
    is_hidden BOOLEAN, 
    description TEXT, 
    default_endpoint TEXT, 
    user_id INTEGER, 
    cluster_name VARCHAR(250), 
    created_by_fk INTEGER, 
    changed_by_fk INTEGER, 
    "offset" INTEGER, 
    cache_timeout INTEGER, perm VARCHAR(1000), filter_select_enabled BOOLEAN, params VARCHAR(1000), 
    PRIMARY KEY (id), 
    CHECK (is_featured IN (0, 1)), 
    CHECK (is_hidden IN (0, 1)), 
    FOREIGN KEY(created_by_fk) REFERENCES ab_user (id), 
    FOREIGN KEY(changed_by_fk) REFERENCES ab_user (id), 
    FOREIGN KEY(cluster_name) REFERENCES clusters (cluster_name), 
    UNIQUE (datasource_name), 
    FOREIGN KEY(user_id) REFERENCES ab_user (id)
);
like image 264
Encore PTL Avatar asked Feb 02 '17 22:02

Encore PTL


2 Answers

SQLite only supports limited ALTER TABLE, so you can't remove the constaint using ALTER TABLE. What you can do to "drop" the column is to rename the table, create a new table with the same schema except for the UNIQUE constraint, and then insert all data into the new table. This procedure is documented in the Making Other Kinds Of Table Schema Changes section of ALTER TABLE documentation.

like image 68
Michal Kottman Avatar answered Oct 30 '22 21:10

Michal Kottman


I just ran into this myself. An easy solution was using DB Browser for SQLite

It let me remove a unique constraint with just a checkbox in a gui.

like image 34
phimath Avatar answered Oct 30 '22 21:10

phimath