You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Airflow启动时sqlalchemy重复列名operator错误?

解决Ubuntu22.04下Airflow db init 报重复列错误的具体步骤

问题重现

按照Ubuntu20.04的Airflow教程在Ubuntu22.04系统安装后,执行airflow db init时触发以下错误:

(airflow_example) yashirq@yashirq:~/airflow_example/bin$ airflow db init
DB: sqlite:////home/yashirq/airflow/airflow.db
[2022-07-16 03:56:06,803] {db.py:1462} INFO - Creating tables
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 13eb55f81627 -> 338e90f54d61, Add ``operator`` and ``queued_dttm`` to ``task_instance`` table
Traceback (most recent call last):
  File "/home/yashirq/airflow_example/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
    self.dialect.do_execute(
  File "/home/yashirq/airflow_example/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: duplicate column name: operator

...(省略中间栈信息)...

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) duplicate column name: operator
[SQL: ALTER TABLE task_instance ADD COLUMN operator VARCHAR(1000)]

错误原因

  1. 之前的数据库初始化未完全成功,残留了包含部分已创建列的SQLite数据库文件,导致迁移脚本重复执行添加列操作。
  2. Ubuntu22.04默认使用Python3.10,而针对20.04的教程可能适配Python3.8,安装的Airflow版本与Python3.10存在兼容性问题,引发迁移逻辑异常。

具体修复步骤

步骤1:删除残留的数据库文件

默认Airflow数据库文件位于$AIRFLOW_HOME/airflow.db,先确认路径:

echo $AIRFLOW_HOME

如果输出为空,默认路径是~/airflow,执行删除命令:

rm ~/airflow/airflow.db

步骤2:重建适配Python3.10的虚拟环境(推荐)

旧虚拟环境可能存在依赖版本不匹配问题,彻底重建:

  1. 退出当前虚拟环境:
deactivate
  1. 删除旧虚拟环境:
rm -rf ~/airflow_example
  1. 创建Python3.10专属虚拟环境:
python3.10 -m venv ~/airflow_example
  1. 激活新虚拟环境:
source ~/airflow_example/bin/activate

步骤3:安装兼容Python3.10的Airflow版本

Airflow 2.3.0及以上版本支持Python3.10,选择稳定版本(如2.4.3)安装:

  1. 更新pip到最新版:
pip install --upgrade pip
  1. 按官方约束安装Airflow:
pip install "apache-airflow==2.4.3" --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-2.4.3/constraints-3.10.txt"

步骤4:重新初始化数据库

设置Airflow工作目录(可选,默认已为~/airflow):

export AIRFLOW_HOME=~/airflow

执行初始化命令:

airflow db init

验证

执行完airflow db init后如果没有报错,说明数据库初始化成功,可以继续创建用户、启动服务等操作。

内容的提问来源于stack exchange,提问作者user8314628

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:12:32