Please tell me how to limit the output of the number of rows in sqlalchemy
?
I have 2 tables, they are linked through many to many:
class Track(Base):
__tablename__ = 'track'
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str]
created_at: Mapped[datetime.date] = mapped_column(default=func.now())
base_collections: Mapped[List['BaseCollection']] = relationship(
secondary=track_base_collection_association, back_populates='tracks'
)
class BaseCollection(Base):
__tablename__ = 'base_collection'
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str]
tracks: Mapped[List['Track']] = relationship(
secondary=track_base_collection_association, back_populates='base_collections'
)
track_base_collection_association = Table(
'track_base_collection_association',
Base.metadata,
Column('id', Integer, primary_key=True),
Column('track_id', ForeignKey('track.id')),
Column('base_collection_id', ForeignKey('base_collection.id'))
)
How do I make a request base_collection.id == 1
, tracks I received no more than 50 pcs?
The limit can be set to receive the base_collection
, but I cannot imagine how the limit can be divided into tracks like this:
select(BaseCollection).where(BaseCollection.id == collection_id).options(joinedload(BaseCollection.base), selectinload(BaseCollection.tracks).limit(limit))