Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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的联接条件出错_Python_Python 3.x_Sqlalchemy - Fatal编程技术网

Python SqlAlchemy的联接条件出错

Python SqlAlchemy的联接条件出错,python,python-3.x,sqlalchemy,Python,Python 3.x,Sqlalchemy,我试图在我的python应用程序上使用SQLAlchemy,但我在多对多关系上遇到了问题。 我有4张桌子: 用户、标志、命令、通道和命令\u通道\u标志 命令\通道\标志包含每个相关表的外键(命令、通道和标志) 用户也有一个标志id作为外键 所以我试着把命令、频道和旗帜联系起来。目的是了解一个命令可以在一个通道上为一个标志运行 我这样做: from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.ext.

我试图在我的python应用程序上使用SQLAlchemy,但我在多对多关系上遇到了问题。 我有4张桌子:

用户、标志、命令、通道和命令\u通道\u标志

命令\通道\标志包含每个相关表的外键(命令、通道和标志)

用户也有一个标志id作为外键

所以我试着把命令、频道和旗帜联系起来。目的是了解一个命令可以在一个通道上为一个标志运行

我这样做:

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'))
但我有一个错误:

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

我知道我的表链接中有一个错误,但我找不到它。

返回\u填充
需要与另一个模型上相关属性的确切名称匹配。在
Channel
中,您有
back\u populates=“channels”
,但在
Commande
中,您有:

channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="commandes")
相反,将
channel=relationship
更改为
channels=relationship


您还需要将其他关系属性更改为
Flag.commandes
Flag.channels
Channel.commandes
Channel.flags
,和
Commande.flags
以匹配
back\u填充的
参数。

表名中似乎有输入错误:
channel\u id=Column(ForeignKey('chanels.id'))
哦,是的,我更改了它,但我有另一个错误(消息已编辑),感谢您的回答。我不明白第二部分。我必须更改哪些属性?
commande
Flag
上的
commandes
channel
channels
Flag
上的
等等。是的,我这样做了,但是当您说还需要更改其他关系属性时,您谈论的是第一个参数吗?例如,在Flag:commandes=relationship(“Commande.flags”,secondary=“commandes\u channels\u flags”,back\u populates=“flags”)上,仅仅是关系属性的名称
commandes=relationship(“Commande”
)…哦,好吧,我不太明白。你说得对,它解决了我的问题。非常感谢