简体   繁体   English

使用SqlAlchemy的连接条件错误

[英]Error on join condition with SqlAlchemy

I'm trying to use SQLAlchemy on my python app but I have a problem with the many to many relationship. 我正在尝试在python应用程序上使用SQLAlchemy,但是多对多关系存在问题。 I have 4 tables: 我有4张桌子:

users, flags, commandes, channels, and commandes_channels_flags 用户,标志,命令,通道和命令_通道_标志

commandes_channels_flags contain a foreign key for each concerned table (commandes, channels and flags) commandes_channels_flags包含每个相关表的外键(commandes,channels和flags)

An user has a flag_id as foreign key too. 用户也具有flag_id作为外键。

So I try to link commandes, channels and flag. 因此,我尝试链接命令,通道和标志。 the objective is to know that a command can run on a channel for a flag. 目的是要知道命令可以在通道上运行以进行标记。

I did this: 我这样做:

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    pseudo = Column(String(50), unique=True, nullable=False)
    flag_id = Column(ForeignKey('flags.id'))


class Flag(Base):
    __tablename__ = 'flags'

    id = Column(Integer, primary_key=True)
    irc_flag = Column(Integer)
    nom = Column(String(50))

    users = relationship("User", backref="flag", order_by="Flag.irc_flag")
    commande = relationship("Commande", secondary="commandes_channels_flags", back_populates="flags")
    channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="flags")


class Channel(Base):
    __tablename__ = 'channels'

    id = Column(Integer, primary_key=True)
    uri = Column(String(50))
    topic = Column(String(255))

    commande = relationship("Commande", secondary="commandes_channels_flags", back_populates="channels")
    flag = relationship("Flag", secondary="commandes_channels_flags", back_populates="channels")


class Commande(Base):
    __tablename__ = 'commandes'

    id = Column(Integer, primary_key=True)
    pattern = Column(String(50))

    channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="commandes")
    flag = relationship("Flag", secondary="commandes_channels_flags", back_populates="commandes")


class CommandeChannelFlag(Base):
    __tablename__ = 'commandes_channels_flags'

    id = Column(Integer, primary_key=True)
    commande_id = Column(ForeignKey('commandes.id'))
    channel_id = Column(ForeignKey('channels.id'))
    flag_id = Column(ForeignKey('flags.id'))

But I have this error: 但是我有这个错误:

sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Commande|commandes' has no property 'channels'

I understand that I have an error in my tables linking but I can't find it. 我了解我的表格链接中有一个错误,但找不到。

back_populates needs to match the exact name of the related property on the other model. back_populates需要与另一个模型上相关属性的确切名称匹配。 In Channel , you have back_populates="channels" , but in Commande , you have: Channel ,您具有back_populates="channels" ,但在Commande ,您具有:

channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="commandes")

Instead, change channel = relationship to channels = relationship . 而是将channel = relationship更改为channels = relationship

You'll also need to change the other relationship properties to Flag.commandes , Flag.channels , Channel.commandes , Channel.flags , and Commande.flags to match your back_populates arguments. 你还需要改变其他关系属性Flag.commandesFlag.channelsChannel.commandesChannel.flagsCommande.flags以符合您back_populates参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 左连接上的SQLAlchemy错误 - SQLAlchemy error on left join 加入条件以急切加载sqlalchemy orm - Join on a condition to eagerly load in sqlalchemy orm SQLAlchemy:如何联接表并按!=条件进行过滤? - SQLAlchemy: how to join tables and filter by != condition? SqlAlchemy无法确定加入条件 - SqlAlchemy can't determine join condition sqlalchemy中具有反射和声明性语法的一对多关系定义给出了连接条件错误 - One To Many Relationship definition with reflection and declarative syntax in sqlalchemy gives join condition error sqlalchemy 错误:AmbiguousForeignKeysError 无法确定关系上的父/子表之间的连接条件 - sqlalchemy error: AmbiguousForeignKeysError Could not determine join condition between parent/child tables on relationship 无法使用多对多连接Sqlalchemy确定连接条件 - Could not determine join condition with many-to-many join Sqlalchemy 在SQLAlchemy中编写联接错误:sqlalchemy.exc.InvalidRequestError: - Writing a join in SQLAlchemy error: sqlalchemy.exc.InvalidRequestError: flask sqlalchemy.exc.NoForeignKeysError NoForeignKeysError:无法确定连接条件 - flask sqlalchemy.exc.NoForeignKeysError NoForeignKeysError: Could not determine join condition SQLAlchemy:通过辅助表以自定义条件进行外部联接 - Sqlalchemy: Outer join with custom condition through a secondary table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM