I apologize if inversion is not the preferred nomenclature that may have interfered with my search. In any case, I am dealing with two declarative sqlalchemy classes, which are many-to-many relationships. The first is an account, and the second is a collection. Users "buy" collections, but I want to show the first 10 collections that the user has not yet purchased .
from sqlalchemy import *
from sqlalchemy.orm import scoped_session, sessionmaker, relation
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
account_to_collection_map = Table('account_to_collection_map', Base.metadata,
Column('account_id', Integer, ForeignKey('account.id')),
Column('collection_id', Integer, ForeignKey('collection.id')))
class Account(Base):
__tablename__ = 'account'
id = Column(Integer, primary_key=True)
email = Column(String)
collections = relation("Collection", secondary=account_to_collection_map)
dyn_coll = relation("Collection", secondary=account_to_collection_map, lazy='dynamic')
def __init__(self, email):
self.email = email
def __repr__(self):
return "<Acc(id=%s email=%s)>" % (self.id, self.email)
class Collection(Base):
__tablename__ = 'collection'
id = Column(Integer, primary_key=True)
slug = Column(String)
def __init__(self, slug):
self.slug = slug
def __repr__(self):
return "<Coll(id=%s slug=%s)>" % (self.id, self.slug)
So, with account.collections I can get all the collections, and with dyn_coll.limit (1) .all () I can apply queries to the list of collections ... but how can I do the opposite? I would like to get the first 10 collections that the account does not .
Any help is incredibly appreciated. Thank!