Repository navigation
Conversation
Adds a new unique= argument to Database.create_table(), Table.create(),
Table.transform() and the table-creating insert/upsert methods
(insert, insert_all, upsert, upsert_all). A string names a single
column and a tuple names a compound constraint spanning several
columns, so unique=["email"] and unique=[("account_id", "email")]
are both supported. The argument is also available as a table
default via db.table(..., unique=[...]).
Also adds a table.uniques introspection property that lists the
UNIQUE constraints on a table, mirroring table.checks.
Refs simonw#558
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #558.
Summary
Adds a
unique=argument for definingUNIQUEconstraints when a table is created, mirroring the existingnot_null=argument.A string names a single column; a tuple names a compound constraint spanning several columns. Both can be combined in one list:
Produces:
Where it is accepted
Database.create_table()Table.create()Table.transform()- adds the constraint to an existing table; repeating a constraint that is already present is a no-opinsert(),insert_all(),upsert()andupsert_all()db.table(..., unique=[...])create(..., transform=True)detects a newly requested constraint and applies it.Also added
table.uniquesintrospection property listing theUNIQUEconstraints on a table, mirroringtable.checks.docs/python-api.rst.Tests
16 new tests in
tests/test_create.pycover single-column, multi-column and compound constraints, constraint enforcement, the bad-column error, persistence via_defaults, transform (including idempotency and renamed columns),transform=True, and the insert/upsert paths.Full suite:
1513 passed, 16 skipped.black,flake8,mypy,pyrightandcog --checkall pass.📚 Documentation preview 📚: https://sqlite-utils--898.org.readthedocs.build/en/898/