Add foreign keys safely in SQLAlchemy

When adding foreign keys in SQLAlchemy migrations, split the operation into two steps to avoid blocking writes on both tables:
1. First create the foreign key constraint without validation
2. Then validate existing data in a separate migration
Bad:
```python
def upgrade():

Install this rule for wispbit

Quick Install

Recommended
View install script

Run this one command to automatically install the rule:

curl -fsSL https://wispbit.com/api/install?rule=sqlalchemy-split-foreign-key | bash

Manual install

1

Copy the rule

---
include: *.py
---
When adding foreign keys in SQLAlchemy migrations, split the operation into two steps to avoid blocking writes on both tables:
1. First create the foreign key constraint without validation
2. Then validate existing data in a separate migration
Bad:
```python
def upgrade():
    # Directly creating a foreign key constraint can block writes on both tables
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id']
    )
```
Good:
```python
# Migration 1: Add foreign key without validation
def upgrade():
    # Create the foreign key constraint without validating existing data
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id'],
        postgresql_not_valid=True
    )
```
```python
# Migration 2: Validate existing data
def upgrade():
    op.execute('ALTER TABLE users VALIDATE CONSTRAINT fk_users_orders')
```
2

Add the rule into your project

Save the copied content as: .wispbit/rules/sqlalchemy-split-foreign-key.md

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: |
                
        When adding foreign keys in SQLAlchemy migrations, split the operation into two steps to avoid blocking writes on both tables:
        
        1. First create the foreign key constraint without validation
        2. Then validate existing data in a separate migration
        
        Bad:
        
        ```python
        def upgrade():
            # Directly creating a foreign key constraint can block writes on both tables
            op.create_foreign_key(
                'fk_users_orders',
                'users',
                'orders',
                ['order_id'],
                ['id']
            )
        ```
        
        Good:
        
        ```python
        # Migration 1: Add foreign key without validation
        
        def upgrade():
            # Create the foreign key constraint without validating existing data
            op.create_foreign_key(
                'fk_users_orders',
                'users',
                'orders',
                ['order_id'],
                ['id'],
                postgresql_not_valid=True
            )
        ```
        
        ```python
        # Migration 2: Validate existing data
        
        def upgrade():
            op.execute('ALTER TABLE users VALIDATE CONSTRAINT fk_users_orders')
        ```
        

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.

When adding foreign keys in SQLAlchemy migrations, split the operation into two steps to avoid blocking writes on both tables:
1. First create the foreign key constraint without validation
2. Then validate existing data in a separate migration
Bad:
```python
def upgrade():
    # Directly creating a foreign key constraint can block writes on both tables
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id']
    )
```
Good:
```python
# Migration 1: Add foreign key without validation
def upgrade():
    # Create the foreign key constraint without validating existing data
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id'],
        postgresql_not_valid=True
    )
```
```python
# Migration 2: Validate existing data
def upgrade():
    op.execute('ALTER TABLE users VALIDATE CONSTRAINT fk_users_orders')
```

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.

When adding foreign keys in SQLAlchemy migrations, split the operation into two steps to avoid blocking writes on both tables:
1. First create the foreign key constraint without validation
2. Then validate existing data in a separate migration
Bad:
```python
def upgrade():
    # Directly creating a foreign key constraint can block writes on both tables
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id']
    )
```
Good:
```python
# Migration 1: Add foreign key without validation
def upgrade():
    # Create the foreign key constraint without validating existing data
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id'],
        postgresql_not_valid=True
    )
```
```python
# Migration 2: Validate existing data
def upgrade():
    op.execute('ALTER TABLE users VALIDATE CONSTRAINT fk_users_orders')
```

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.

When adding foreign keys in SQLAlchemy migrations, split the operation into two steps to avoid blocking writes on both tables:
1. First create the foreign key constraint without validation
2. Then validate existing data in a separate migration
Bad:
```python
def upgrade():
    # Directly creating a foreign key constraint can block writes on both tables
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id']
    )
```
Good:
```python
# Migration 1: Add foreign key without validation
def upgrade():
    # Create the foreign key constraint without validating existing data
    op.create_foreign_key(
        'fk_users_orders',
        'users',
        'orders',
        ['order_id'],
        ['id'],
        postgresql_not_valid=True
    )
```
```python
# Migration 2: Validate existing data
def upgrade():
    op.execute('ALTER TABLE users VALIDATE CONSTRAINT fk_users_orders')
```

File Path Patterns:

*.py