Skip to content Skip to sidebar Skip to footer

Sqlalchemy: Order Of Query Result Unexpected

I'm using SQLAlchemy with MySQL and have a table with two foreign keys: class CampaignCreativeLink(db.Model): __tablename__ = 'campaign_creative_links' campaign_id = db.Column(

Solution 1:

A table is a set of rows and are therefore not guaranteed to have any order unless you specify ORDER BY.

In MySQL (InnoDB), the primary key acts as the clustered index. This means that the rows are physically stored in the order specified by the primary key, in this case (campaign_id, created_id), regardless of the order of insertion. This is usually the order the rows are returned in if you don't specify an ORDER BY.

If you need your rows returned in a certain order, specify ORDER BY when you query.

Post a Comment for "Sqlalchemy: Order Of Query Result Unexpected"