"""修改slug字段为可空的迁移脚本""" import sys import os # 添加项目根目录到系统路径 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from app import create_app from models import db from sqlalchemy import text # 创建应用上下文 app = create_app() with app.app_context(): # 使用原生SQL修改列属性 try: # MySQL语法:修改slug列为可空 with db.engine.connect() as connection: connection.execute(text('ALTER TABLE sites MODIFY COLUMN slug VARCHAR(100) NULL')) connection.commit() print("slug field successfully updated to nullable!") except Exception as e: print(f"Migration failed: {str(e)}") print("Note: If the field is already nullable, you can ignore this error.")