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

Airflow执行task test报错:xcom.execution_date列不存在求助

问题解决:Airflow LocalExecutor模式下airflow tasks test报错column xcom.execution_date does not exist

环境背景

基于官方Airflow Docker Compose配置,将CeleryExecutor替换为LocalExecutor搭建轻量容器,相关配置如下:

Docker Compose配置(docker-compose.yaml)

---
version: '3'
x-airflow-common:
  &airflow-common
  build:
    context: .
    dockerfile: ./Dockerfile
  environment:
    &airflow-common-env
    AIRFLOW__CORE__EXECUTOR: LocalExecutor
    AIRFLOW__CORE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres/airflow
    AIRFLOW__CELERY__RESULT_BACKEND: db+postgresql://airflow:airflow@postgres/airflow
    AIRFLOW__CORE__FERNET_KEY: ''
    AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'true'
    AIRFLOW__CORE__LOAD_EXAMPLES: 'false'
    AIRFLOW__API__AUTH_BACKEND: 'airflow.api.auth.backend.basic_auth'
    _PIP_ADDITIONAL_REQUIREMENTS: ${_PIP_ADDITIONAL_REQUIREMENTS:- matplotlib }

  volumes:
    - ./dags:/opt/airflow/dags
    - ./logs:/opt/airflow/logs
    - ./plugins:/opt/airflow/plugins


  user: "${AIRFLOW_UID:-50000}:0"
  depends_on:
    &airflow-common-depends-on
    postgres:
      condition: service_healthy

services:
  postgres:
    image: postgres:13
    environment:
      POSTGRES_USER: airflow
      POSTGRES_PASSWORD: airflow
      POSTGRES_DB: airflow
    volumes:
      - postgres-db-volume:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "airflow"]
      interval: 5s
      retries: 5
    restart: always

  airflow-webserver:
    <<: *airflow-common
    command: webserver
    ports:
      - 8080:8080
    healthcheck:
      test: ["CMD", "curl", "--fail", "http://localhost:8080/health"]
      interval: 10s
      timeout: 10s
      retries: 5
    restart: always
    depends_on:
      <<: *airflow-common-depends-on
      airflow-init:
        condition: service_completed_successfully

  airflow-scheduler:
    <<: *airflow-common
    command: scheduler
    healthcheck:
      test: ["CMD-SHELL", 'airflow jobs check --job-type SchedulerJob --hostname "$${HOSTNAME}"']
      interval: 10s
      timeout: 10s
      retries: 5
    restart: always
    depends_on:
      <<: *airflow-common-depends-on
      airflow-init:
        condition: service_completed_successfully

  airflow-init:
    <<: *airflow-common
    entrypoint: /bin/bash
    # yamllint disable rule:line-length
    command:
     .....long bash script
    # yamllint enable rule:line-length
    environment:
      <<: *airflow-common-env
      _AIRFLOW_DB_UPGRADE: 'true'
      _AIRFLOW_WWW_USER_CREATE: 'true'
      _AIRFLOW_WWW_USER_USERNAME: ${_AIRFLOW_WWW_USER_USERNAME:-airflow}
      _AIRFLOW_WWW_USER_PASSWORD: ${_AIRFLOW_WWW_USER_PASSWORD:-airflow}
    user: "0:0"
    volumes:
      - .:/sources

  airflow-cli:
    <<: *airflow-common
    profiles:
      - debug
    environment:
      <<: *airflow-common-env
      CONNECTION_CHECK_MAX_COUNT: "0"
    # Workaround for entrypoint issue. See: https://github.com/apache/airflow/issues/16252
    command:
      - bash
      - -c
      - airflow

volumes:
  postgres-db-volume:

Dockerfile配置

FROM apache/airflow:2.2.3

ENV AIRFLOW_HOME=/opt/airflow

WORKDIR $AIRFLOW_HOME

USER root
RUN apt-get update -qq && apt-get install vim -qqq && apt-get install -y python3-pip

COPY requirements.txt .

RUN python3 -m pip install --upgrade pip

RUN python3 -m pip install --no-cache-dir -r requirements.txt

USER $AIRFLOW_UID

问题现象

所有容器状态均显示健康,执行airflow dags list能正常返回预期的DAG列表,但执行airflow tasks test {{dag}} {{task}} {{date}}命令时,出现如下错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column xcom.execution_date does not exist

解决方案

该错误源于Airflow 2.x版本中xcom表的execution_date字段已被移除,改为通过run_id关联dag_run表获取执行日期。对应解决方法如下:

1. 手动执行数据库迁移升级

虽配置了_AIRFLOW_DB_UPGRADE: 'true',但可能因初始化脚本或镜像构建问题导致迁移未完全执行:

  • 进入Airflow容器(如webserver):
    docker-compose exec airflow-webserver bash
    
  • 执行数据库迁移命令:
    airflow db upgrade
    
  • 重启所有Airflow服务容器:
    docker-compose restart airflow-webserver airflow-scheduler
    

2. 检查并修复自定义代码/插件中的旧逻辑

若DAG代码或自定义插件中存在直接访问xcom.execution_date的逻辑,需替换为Airflow 2.x兼容写法:

# 错误写法(Airflow 1.x兼容)
xcom_records = TaskInstance.xcom_pull(task_ids="my_task", execution_date=execution_date)

# 正确写法(Airflow 2.x)
from airflow.models import DagRun
dag_run = DagRun.find(dag_id="my_dag", execution_date=execution_date)[0]
xcom_records = TaskInstance.xcom_pull(task_ids="my_task", run_id=dag_run.run_id)

3. 清理残留数据卷后重新初始化(极端情况)

若上述方法无效,可清理PostgreSQL数据卷后重新构建容器:

  • 停止并删除容器及数据卷:
    docker-compose down -v
    
  • 重新构建并启动服务:
    docker-compose up --build -d
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:50:25