simonw / simonw/sqlite-utils

Ability to define unique columns when creating a table

Open
#558 0 comments 0 reactions 0 assignees View on GitHub

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.