🔧 Fix Railway migration DuplicateColumn error with smart column check

## Migration Fix for Railway Database
- Railway PostgreSQL already has access_url_name/display_url_name columns
- Previous migration tried to add existing columns → DuplicateColumn error
- New migration checks if columns exist before adding them

## Smart Migration Logic
 Check information_schema for existing columns
 Add columns only if they don't exist
 Skip if columns already present (Railway case)
 Works for both fresh and existing databases

## Error Fixed
 Was: column 'access_url_name' of relation 'agents_agent' already exists
 Now: Migration succeeds regardless of existing schema state

## Result
- Railway deployment will complete successfully
- populate_agents will run and create all 5 agents
- Marketplace will show agents again

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude 2025-08-04 16:20:52 +05:30
parent 54d7d3e506
commit f10a097d9c

View File

@ -3,6 +3,42 @@
from django.db import migrations, models
def check_and_add_fields(apps, schema_editor):
"""Add fields only if they don't already exist"""
db_alias = schema_editor.connection.alias
# Check if columns already exist in the database
with schema_editor.connection.cursor() as cursor:
cursor.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'agents_agent'
AND column_name IN ('access_url_name', 'display_url_name')
""")
existing_columns = [row[0] for row in cursor.fetchall()]
# Add access_url_name if it doesn't exist
if 'access_url_name' not in existing_columns:
cursor.execute("""
ALTER TABLE agents_agent
ADD COLUMN access_url_name VARCHAR(100) DEFAULT '' NOT NULL
""")
# Add display_url_name if it doesn't exist
if 'display_url_name' not in existing_columns:
cursor.execute("""
ALTER TABLE agents_agent
ADD COLUMN display_url_name VARCHAR(100) DEFAULT '' NOT NULL
""")
def reverse_check_and_add_fields(apps, schema_editor):
"""Remove fields if they exist"""
with schema_editor.connection.cursor() as cursor:
cursor.execute("ALTER TABLE agents_agent DROP COLUMN IF EXISTS access_url_name")
cursor.execute("ALTER TABLE agents_agent DROP COLUMN IF EXISTS display_url_name")
class Migration(migrations.Migration):
dependencies = [
@ -10,14 +46,5 @@ class Migration(migrations.Migration):
]
operations = [
migrations.AddField(
model_name='agent',
name='access_url_name',
field=models.CharField(max_length=100, blank=True, default=''),
),
migrations.AddField(
model_name='agent',
name='display_url_name',
field=models.CharField(max_length=100, blank=True, default=''),
),
migrations.RunPython(check_and_add_fields, reverse_check_and_add_fields),
]