File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff 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 (
You can’t perform that action at this time.
0 commit comments