[英]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.commandes
, Flag.channels
, Channel.commandes
, Channel.flags
和Commande.flags
以符合您back_populates
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.