I don't understand that error - you're asking it to join along predefined
relationships, so it shouldn't need to search for foreign keys at query
time. "Category.products" is a relationship set up as the backref of
Product.categories.

Here's a working example. I had to add the definition of the many-to-many
table.


import sqlalchemy as sa
from sqlalchemy import Column, Integer, UnicodeText, ForeignKey
import sqlalchemy.orm as saorm
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class Brand(Base):
    __tablename__ = 'brand'

    id = Column(Integer, primary_key=True)
    name = Column(UnicodeText, nullable=False)
    slug = Column(UnicodeText, nullable=False, unique=True)


class Category(Base):
    __tablename__ = 'category'

    id = Column(Integer, primary_key=True)
    name = Column(UnicodeText, nullable=False)

product_category = sa.Table(
    'product_category',
    Base.metadata,
    Column('product_id', ForeignKey('product.id')),
    Column('category_id', ForeignKey('category.id')),
)

class Product(Base):
    __tablename__ = 'product'

    id = Column(Integer, primary_key=True)
    brand_id = Column(Integer, ForeignKey('brand.id'), nullable=False)

    name = Column(UnicodeText, nullable=False)

    brand = relationship('Brand', backref='products')
    categories = relationship('Category', backref='products',
secondary=product_category)   # many-to-many relationship

if __name__ == '__main__':
    engine = sa.create_engine('sqlite://')
    Base.metadata.create_all(bind=engine)
    Session = saorm.sessionmaker(bind=engine)
    session = Session()
    brand_slug = 'some_slug'
    q = session.query(Category).join('products',
'brand').filter(Brand.slug==brand_slug)
    print q
    print q.all()



On Wed, Mar 2, 2016 at 6:09 AM, sector119 <sector...@gmail.com> wrote:

> With that query I get:
> InvalidRequestError: Could not find a FROM clause to join from.  Tried
> joining to <class 'supplements.models.Product'>, but got: Can't find any
> foreign key relationships between 'category' and 'product'.
>
> Product and category model has many to many relationship
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to