Error on join condition with SqlAlchemy
<p>I'm trying to use SQLAlchemy on my python app but I have a problem with the many to many relationship.
I have 4 tables:</p>

<p>users, flags, commandes, channels, and commandes_channels_flags</p>

<p>commandes_channels_flags contain a foreign key for each concerned table (commandes, channels and flags)</p>

<p>An user has a flag_id as foreign key too.</p>

<p>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.</p>

<p>I did this:</p>

<pre><code>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'))
</code></pre>

<p>But I have this error:</p>

<pre><code>sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Commande|commandes' has no property 'channels'
</code></pre>

<p>I understand that I have an error in my tables linking but I can't find it.</p>

<p><code>back_populates</code> needs to match the exact name of the related property on the other model. In <code>Channel</code>, you have <code>back_populates="channels"</code>, but in <code>Commande</code>, you have:</p>

<pre><code>channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="commandes")
</code></pre>

<p>Instead, change <code>channel = relationship</code> to <code>channels = relationship</code>.</p>

<p>You'll also need to change the other relationship properties to <code>Flag.commandes</code>, <code>Flag.channels</code>, <code>Channel.commandes</code>, <code>Channel.flags</code>, and <code>Commande.flags</code> to match your <code>back_populates</code> arguments.</p>

python python-3.x sqlalchemy