Ability to define unique columns when creating a table
Open
Nobody has claimed this yet.
enhancement
python-library
- Dominant language
- Python
- Stars
- 2.2k
- Forks
- 172
- Avg merge
- 9m
- Merged PRs (30d)
- 1
Description
When creating a new table, it would be good to have an option to set unique columns similar to how not_null is set.
from sqlite_utils import Database
columns = {"mRID": str, "name": str}
db = Database("example.db")
db["ExampleTable"].create(columns, pk="mRID", not_null=["mRID"], if_not_exists=True)
db["ExampleTable"].create_index(["mRID"], unique=True, if_not_exists=True)
So something like this would add the UNIQUE flag to the table definition.
db["ExampleTable"].create(columns, pk="mRID", not_null=["mRID"], unique=["mRID"], if_not_exists=True)
CREATE TABLE ExampleTable (
mRID TEXT PRIMARY KEY
NOT NULL
UNIQUE,
name TEXT
);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Database table create() entry point shown in the issue and trace how not_null and pk are turned into the CREATE TABLE statement. Add support for the unique option and verify that the generated schema includes UNIQUE for the requested columns while preserving the existing create_index behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100