Skip to content

Commit 1c39ffa

Browse files
authored
Merge pull request #816 from seapagan/fix/apikey-userid-index
fix: make api_keys.user_id index explicit in model
2 parents 63a0dcb + 69b662e commit 1c39ffa

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

SECURITY-REVIEW.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,19 @@
271271

272272
### 16. Missing Database Index on Foreign Key
273273

274+
> [!NOTE]
275+
> **Done**: The index already existed in the database (created by the original
276+
> migration) but wasn't marked in the model. Added `index=True` to match the
277+
> actual database schema. See PR #816.
278+
274279
**Location**: `app/models/api_key.py:22`
275280

276-
- **Issue**: `user_id` foreign key lacks index. Queries filtering by user_id
277-
(like `get_user_api_keys_`) will do full table scans as API key count grows.
278-
- **Impact**: Performance degradation over time, slow queries.
279-
- **Fix**: Add `index=True` to `user_id` field:
281+
- **Issue**: `user_id` foreign key lacks index in the model definition. The
282+
index was created by the migration but not reflected in the model, causing
283+
a mismatch that could confuse `alembic autogenerate`.
284+
- **Impact**: Model/database schema mismatch; the index exists in the database
285+
but wasn't explicit in the code.
286+
- **Fix**: Add `index=True` to `user_id` field to match the database schema.
280287

281288
```python
282289
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)

app/models/api_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApiKey(Base):
1818
id: Mapped[uuid.UUID] = mapped_column(
1919
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
2020
)
21-
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
21+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
2222
key: Mapped[str] = mapped_column(String(255), unique=True, index=True)
2323
name: Mapped[str] = mapped_column(String(50))
2424
created_at: Mapped[datetime] = mapped_column(

0 commit comments

Comments
 (0)