Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy与airflow中的task_实例表的关系_Python_Sqlalchemy_Airflow - Fatal编程技术网

Python SQLAlchemy与airflow中的task_实例表的关系

Python SQLAlchemy与airflow中的task_实例表的关系,python,sqlalchemy,airflow,Python,Sqlalchemy,Airflow,我正在使用airflow,我希望能够跟踪表airflow.file_list中给定任务实例生成的所有文件,该表是airflow(在postgres上运行)使用的同一数据库的一部分。使用SQLAlchemy,我的文件列表表有以下映射器: from airflow.models import Base class MySourceFile(Base): """ SQLAlchemy mapper class for the file_list table entries.""" _

我正在使用airflow,我希望能够跟踪表
airflow.file_list
中给定任务实例生成的所有文件,该表是airflow(在postgres上运行)使用的同一数据库的一部分。使用SQLAlchemy,我的
文件列表
表有以下映射器:

from airflow.models import Base

class MySourceFile(Base):
    """ SQLAlchemy mapper class for the file_list table entries."""
    __table__ = Table('file_list', Base.metadata,
        Column('UID', Integer, primary_key=True),
        Column('task_id', String(_ID_LEN), nullable=False),
        Column('dag_id', String(_ID_LEN), nullable=False),
        Column('execution_date', DateTime, nullable=False),
        Column('file_path', String(_ID_LEN), nullable=False),
        Column('file_sha256', String(_ID_LEN), nullable=False),
        ForeignKeyConstraint(
            ['task_id', 'dag_id', 'execution_date'],
            ['task_instance.task_id', 'task_instance.dag_id', 'task_instance.execution_date']
        ),
        extend_existing=True,
    )

    instance_task = relationship(
    TaskInstance,
    primaryjoin=and_(
        TaskInstance.task_id == __table__.c.task_id,
        TaskInstance.dag_id == __table__.c.dag_id,
        TaskInstance.execution_date == __table__.c.execution_date
    ),
    viewonly=True,
    foreign_keys=[__table__.c.task_id, __table__.c.dag_id, __table__.c.execution_date]
)
我正在从
aiffort.modles
导入声明性base,因为我已经读到交互映射程序必须共享相同的base实例。在上面的代码片段中,我希望
instance\u task
引用创建文件的task\u实例。“我的表”中的表列
task\u id
dag\u id
execution\u date
aiffort.task\u实例中镜像主键。不幸的是,在运行airflow服务器时,出现以下错误:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|MySourceFile|file_list'. Original exception was: Can't determine relationship direction for relationship 'MySourceFile.instance_task' - foreign key columns are present in neither the parent nor the child's mapped tables sqlalchemy.exc.InvalidRequestError:一个或多个映射程序未能初始化-无法继续初始化其他映射程序。触发映射程序:“映射程序| MySourceFile |文件|列表”。原始异常是:无法确定关系“MySourceFile.instance_task”的关系方向-外键列既不存在于父级映射表中,也不存在于子级映射表中
如果可能的话,我不想修改气流源。提前感谢您的帮助。

首先,像这样指定
\uuuuu table\uuuu
很奇怪(我以前从未见过),一般来说,任何以和下划线开头的内容都是私有的,应该避免使用

您需要指定
task\u id
是外键。大概是这样的:

task_id = Column(String, ForeignKey('task.id'))

如果你没有看到它们,那么它们是有用的。该模式可能有助于创建从任务实例到自定义文件的关系,而无需更改TaskInstance模型。

如文档中所述,以我所做的方式指定列/表。我以前使用过你建议的陈述式方法,但没有成功。啊,对了。不过,您应该能够在此模式下将
ForeignKey(task\u instance.id)
传递给Column()调用。我完全忽略了这是一个复合外键。这不是你想要的答案,但安装气流和使用你的模型都很好。我要确保使用中的表最终确实具有fk约束。您使用的是
extend\u existing
,因此,即使该表存在于此定义之前的元数据中,并且没有fk,您也应该被覆盖,但请进行双重检查。