Skip to content

Commit 69f742c

Browse files
committed
Update docs
1 parent 8424db0 commit 69f742c

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

docs/advanced/primary_key.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22

33
由于在 python 内部 `id` 为关键字,因此,我们设定默认主键入参为 `pk`。这仅用于函数入参,并不要求模型主键必须定义为 `pk`
44

5-
```py title="e.g." hl_lines="2"
6-
async def delete(self, db: AsyncSession, primary_key: int) -> int:
7-
return self.delete_model(db, pk=primary_key)
8-
```
9-
10-
## 主键定义
11-
125
!!! tip 自动主键
136

147
我们在 SQLAlchemy CRUD Plus 内部通过 [inspect()](https://docs.sqlalchemy.org/en/20/core/inspection.html) 自动搜索表主键,
158
而非强制绑定主键列必须命名为 `id`
169

17-
```py title="e.g." hl_lines="4"
10+
## 单个主键
11+
12+
```py title="e.g."
1813
class ModelIns(Base):
1914
# define primary_key
2015
primary_key: Mapped[int] = mapped_column(primary_key=True, index=True, autoincrement=True)
16+
17+
18+
class ModelIns2(Base):
19+
# define primary_key
20+
primary_key: Mapped[str] = mapped_column(primary_key=True, index=True)
21+
```
22+
23+
## 复合主键
24+
25+
```python title="e.g."
26+
class ModelIns(Base):
27+
primary_key: Mapped[int] = mapped_column(primary_key=True, index=True, autoincrement=True)
28+
primary_key2: Mapped[str] = mapped_column(primary_key=True, index=True)
2129
```

docs/usage/delete_model.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ class CRUDIns(CRUDPlus[ModelIns]):
2424
async def delete_model(
2525
self,
2626
session: AsyncSession,
27-
pk: int,
27+
pk: Any | Sequence[Any],
2828
flush: bool = False,
2929
commit: bool = False,
3030
) -> int:
3131
```
3232

3333
**Parameters:**
3434

35-
| Name | Type | Description | Default |
36-
|---------|--------------|----------------------------------|---------|
37-
| session | AsyncSession | 数据库会话 | 必填 |
38-
| pk | int | [主键](../advanced/primary_key.md) | 必填 |
39-
| flush | bool | [冲洗](../advanced/flush.md) | `False` |
40-
| commit | bool | [提交](../advanced/commit.md) | `False` |
35+
| Name | Type | Description | Default |
36+
|---------|--------------------------|----------------------------------|---------|
37+
| session | AsyncSession | 数据库会话 | 必填 |
38+
| pk | `Any `\| `Sequence[Any]` | [主键](../advanced/primary_key.md) | 必填 |
39+
| flush | bool | [冲洗](../advanced/flush.md) | `False` |
40+
| commit | bool | [提交](../advanced/commit.md) | `False` |
4141

4242
**Returns:**
4343

docs/usage/select_model.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ class CRUDIns(CRUDPlus[ModelIns]):
2424
async def select_model(
2525
self,
2626
session: AsyncSession,
27-
pk: int,
27+
pk: Any | Sequence[Any],
2828
*whereclause: ColumnExpressionArgument[bool],
2929
) -> Model | None:
3030
```
3131

3232
**Parameters:**
3333

34-
| Name | Type | Description | Default |
35-
|--------------|----------------------------------|------------------------------------------------------------------------------------------------------|---------|
36-
| session | AsyncSession | 数据库会话 | 必填 |
37-
| pk | int | [主键](../advanced/primary_key.md) | 必填 |
34+
| Name | Type | Description | Default |
35+
|--------------|----------------------------------|-----------------------------------------------------------------------------------------------------|---------|
36+
| session | AsyncSession | 数据库会话 | 必填 |
37+
| pk | `Any `\| `Sequence[Any]` | [主键](../advanced/primary_key.md) | 必填 |
3838
| *whereclause | `ColumnExpressionArgument[bool]` | 等同于 [SQLAlchemy where](https://docs.sqlalchemy.org/en/20/tutorial/data_select.html#the-where-clause) | |
3939

4040
**Returns:**

docs/usage/update_model.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CRUDIns(CRUDPlus[ModelIns]):
3131
async def update_model(
3232
self,
3333
session: AsyncSession,
34-
pk: int,
34+
pk: Any | Sequence[Any],
3535
obj: UpdateSchema | dict[str, Any],
3636
flush: bool = False,
3737
commit: bool = False,
@@ -44,7 +44,7 @@ async def update_model(
4444
| Name | Type | Description | Default |
4545
|---------|-------------------------------|----------------------------------|---------|
4646
| session | AsyncSession | 数据库会话 | 必填 |
47-
| pk | int | [主键](../advanced/primary_key.md) | 必填 |
47+
| pk | `Any `\| `Sequence[Any]` | [主键](../advanced/primary_key.md) | 必填 |
4848
| obj | `TypeVar `\|` dict[str, Any]` | 更新数据参数 | 必填 |
4949
| flush | bool | [冲洗](../advanced/flush.md) | `False` |
5050
| commit | bool | [提交](../advanced/commit.md) | `False` |

sqlalchemy_crud_plus/crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _get_pk_filter(self, pk: Any | Sequence[Any]) -> list[bool]:
4141
"""
4242
Get the primary key filter(s).
4343
44-
:param pk:
44+
:param pk: Single value for simple primary key, or tuple for composite primary key.
4545
:return:
4646
"""
4747
if isinstance(self.primary_key, list):

tests/test_update.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
from random import choice
4-
53
import pytest
64

75
from sqlalchemy_crud_plus import CRUDPlus

0 commit comments

Comments
 (0)