Skip to content

Commit 5a17728

Browse files
committed
update docs for the unique functionality
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent cf0fd27 commit 5a17728

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Thank you for your interest in contributing to SQLiter! We welcome
55
all contributions, big or small.
66

77
If you are not sure where to start, please take a look at the [open
8-
issues](https://github.com/seapagan/sqliter/issues). If you have an
8+
issues](https://github.com/seapagan/sqliter-py/issues). If you have an
99
idea for a new feature or would like to report a bug, please open a new issue.
1010

1111
We also welcome contributions to the documentation. If you find any errors or
@@ -34,8 +34,8 @@ If you already have a Python version installed, uv will use this.
3434

3535
Before you start contributing, please make sure you have read and understood our
3636
[Code of
37-
Conduct](https://github.com/seapagan/sqliter/blob/main/CODE_OF_CONDUCT.md) and
38-
[License](https://github.com/seapagan/sqliter/blob/main/LICENSE).
37+
Conduct](https://github.com/seapagan/sqliter-py/blob/main/CODE_OF_CONDUCT.md) and
38+
[License](https://github.com/seapagan/sqliter-py/blob/main/LICENSE).
3939

4040
To get started, follow these steps:
4141

@@ -182,14 +182,14 @@ Here are some guidelines to follow when contributing to SQLiter:
182182
- Make sure your code passes all tests before submitting a pull request.
183183
- Document your code using
184184
[docstrings](https://www.python.org/dev/peps/pep-0257/).
185-
- Use [GitHub issues](https://github.com/seapagan/sqliter/issues)
185+
- Use [GitHub issues](https://github.com/seapagan/sqliter-py/issues)
186186
to report bugs or suggest new features.
187187

188188
## Contact
189189

190190
If you have any questions or need help with contributing, please contact me
191191
**@seapagan** on GitHub. You can also use the [GitHub
192-
Discussions](https://github.com/seapagan/sqliter/discussions)
192+
Discussions](https://github.com/seapagan/sqliter-py/discussions)
193193
feature.
194194

195195
Happy contributing!

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Website](https://sqliter.grantramsay.dev)
4949
- Table creation based on Pydantic models
5050
- Automatic primary key generation
5151
- User defined indexes on any field
52+
- Set any field as UNIQUE
5253
- CRUD operations (Create, Read, Update, Delete)
5354
- Chained Query building with filtering, ordering, and pagination
5455
- Transaction support

docs/guide/data-ops.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ print(f"Name: {result.name}, Age: {result.age}, Email: {result.email}")
2323
> [!IMPORTANT]
2424
>
2525
> The `insert()` method will raise a `RecordInsertionError` if you try to insert
26-
> a record with a primary key that already exists in the table. Also, if the
27-
> table does not exist, a `RecordInsertionError` will be raised.
26+
> a record with a primary key that already exists in the table or if the table
27+
> does not exist.
2828
2929
## Querying Records
3030

@@ -64,6 +64,16 @@ user.age = 26
6464
db.update(user)
6565
```
6666

67+
> [!IMPORTANT]
68+
>
69+
> The model you pass must have a primary key value set, otherwise an error will
70+
> be raised. In other words, you use the instance of a model returned by the
71+
> `insert()` method to update the record as it has the primary key value set,
72+
> not the original instance you passed to `insert()`.
73+
>
74+
> You can also set the primary key value on the model instance manually before
75+
> calling `update()` if you have that.
76+
6777
## Deleting Records
6878

6979
To delete a record from the database, you need to pass the model class and the

docs/guide/models.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ to the primary key index (`pk`) that is automatically created.
7676
> You can specify both `indexes` and `unique_indexes` in the `Meta` class if you
7777
> need to.
7878
79+
### Unique Fields
80+
81+
You can also specify that a field should be all unique values by using the
82+
`Unique()` method from the `sqliter.model` module. This will ensure that all
83+
values in this field are unique.
84+
85+
```python
86+
from typing import Annotated
87+
from sqliter.model import BaseDBModel, Unique
88+
89+
class User(BaseDBModel):
90+
name: str
91+
age: int
92+
email: Annotated[str, Unique()]
93+
```
94+
95+
This will raise either a `RecordInsertionError` or `RecordUpdateError` if you
96+
try to insert or update a record with a duplicate value in the chosen field.
97+
98+
> [!TIP]
99+
>
100+
> Using `Annotated` is optional, but without it your code wil not pass
101+
> type-checking with `mypy`. It will work fine at runtime but is not recommended:
102+
>
103+
> ```python
104+
> email: str = Unique()
105+
>
106+
>```
107+
>
108+
> This will give the following Mypy error:
109+
>
110+
> ```pre
111+
> error: Incompatible types in assignment (expression has type "Unique", variable has type "str") [assignment]
112+
>```
113+
79114
### Custom Table Name
80115
81116
By default, the table name will be the same as the model name, converted to

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ database-like format without needing to learn SQL or use a full ORM.
4040
- Table creation based on Pydantic models
4141
- Automatic primary key generation
4242
- User defined indexes on any field
43+
- Set any field as UNIQUE
4344
- CRUD operations (Create, Read, Update, Delete)
4445
- Chained Query building with filtering, ordering, and pagination
4546
- Transaction support

tests/test_unique.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
from sqliter import SqliterDB
88
from sqliter.exceptions import RecordInsertionError, RecordUpdateError
9-
from sqliter.model import BaseDBModel
10-
from sqliter.model.unique import Unique
9+
from sqliter.model import BaseDBModel, Unique
1110

1211

1312
class TestUnique:

0 commit comments

Comments
 (0)