Verify query patterns are covered by an index in SQLAlchemy

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))

Install this rule for wispbit

Add this rule to wispbit and it will run when you open a pull request

Install this rule for Coderabbit

Copy the configuration below and add it to your repository as .coderabbit.yml in your project root.

reviews:
  path_instructions:
    - path: "*.py"
      instructions: |
                
        Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
        
        Bad:
        
        ```python
        # queries.py
        def get_user_by_email(session: Session, email: str):
            # Will perform full table scan - no index on email
            return session.scalar(select(User).where(User.email == email))
        
        # models.py
        class User(Base):
            __tablename__ = 'users'
        
            id: Mapped[int] = mapped_column(primary_key=True)
            email: Mapped[str] = mapped_column(String(255))  # No index
            status: Mapped[str] = mapped_column(String(50))
            created_at: Mapped[datetime] = mapped_column(DateTime)
        ```
        
        Good:
        
        ```python
        # queries.py
        def get_user_by_email(session: Session, email: str):
            # Is covered by an index
            return session.scalar(select(User).where(User.email == email))
        
        # models.py
        class User(Base):
            __tablename__ = 'users'
        
            id: Mapped[int] = mapped_column(primary_key=True)
            email: Mapped[str] = mapped_column(String(255), index=True)
            status: Mapped[str] = mapped_column(String(50))
            created_at: Mapped[datetime] = mapped_column(DateTime)
        ```
        

Install this rule for Greptile

Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

File Path Patterns:

*.py

Install this rule for GitHub Copilot

Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

File Path Patterns:

*.py

Install this rule for Graphite Diamond

Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

File Path Patterns:

*.py

Use with Cline

Copy the rule below and ask Cline to review your code using this rule

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

Use with OpenAI Codex

Copy the rule below and ask OpenAI Codex to review your code using this rule

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

Use with Cursor

Copy the rule below and ask Cursor to review your code using this rule

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

Use with Claude Code

Copy the rule below and ask Claude Code to review your code using this rule

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```

Install this rule for Windsurf

To set up rules for Windsurf Reviews, please see this documentation

Ensure that SQLAlchemy query patterns are covered by appropriate database indexes.
Bad:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Will perform full table scan - no index on email
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255))  # No index
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```
Good:
```python
# queries.py
def get_user_by_email(session: Session, email: str):
    # Is covered by an index
    return session.scalar(select(User).where(User.email == email))
# models.py
class User(Base):
    __tablename__ = 'users'
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(String(255), index=True)
    status: Mapped[str] = mapped_column(String(50))
    created_at: Mapped[datetime] = mapped_column(DateTime)
```