add_sub_relation_column.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import pymysql
  2. DB_CONFIG = {
  3. "host": "rm-f8ze60yirdj8786u2.mysql.rds.aliyuncs.com",
  4. "port": 3306,
  5. "user": "root",
  6. "password": "csqz@20255",
  7. "db": "csqz-client",
  8. "charset": "utf8mb4",
  9. "cursorclass": pymysql.cursors.DictCursor
  10. }
  11. def migrate():
  12. conn = pymysql.connect(**DB_CONFIG)
  13. try:
  14. with conn.cursor() as cursor:
  15. print("Checking family_relation_info schema...")
  16. cursor.execute("DESCRIBE family_relation_info")
  17. columns = [col['Field'] for col in cursor.fetchall()]
  18. if 'sub_relation_type' not in columns:
  19. print("Adding 'sub_relation_type' column...")
  20. cursor.execute("ALTER TABLE family_relation_info ADD COLUMN sub_relation_type INT DEFAULT 0 COMMENT '子关系类型'")
  21. print("Column added.")
  22. else:
  23. print("'sub_relation_type' column already exists.")
  24. conn.commit()
  25. except Exception as e:
  26. print(f"Migration failed: {e}")
  27. finally:
  28. conn.close()
  29. if __name__ == "__main__":
  30. migrate()