a | b | |
---|
| 0 | + | ########### Regular SQLAlchemy works correctly |
---|
| 0 | + | from sqlalchemy import * |
---|
| 0 | + | from sqlalchemy.orm import * |
---|
| 0 | + | from sqlalchemy.ext.declarative import declarative_base |
---|
| 0 | + | |
---|
| 0 | + | Model = declarative_base("sqlite://") |
---|
| 0 | + | |
---|
| 0 | + | class Parent(Model): |
---|
| 0 | + | __tablename__ = 'parents' |
---|
| 0 | + | id = Column(Integer, primary_key=True) |
---|
| 0 | + | |
---|
| 0 | + | class Child(Model): |
---|
| 0 | + | __tablename__ = 'children' |
---|
| 0 | + | id = Column(Integer, primary_key=True) |
---|
| 0 | + | parent_id = Column(Integer, ForeignKey('parents.id')) |
---|
| 0 | + | parent = relationship(Parent, backref=backref('children', order_by=id.desc())) |
---|
| 0 | + | |
---|
| 0 | + | def __repr__(self): |
---|
| 0 | + | return "<Child id=%s>" % self.id |
---|
| 0 | + | |
---|
| 0 | + | Model.metadata.create_all() |
---|
| 0 | + | parent = Parent() |
---|
| 0 | + | child1 = Child(parent=parent) |
---|
| 0 | + | child2 = Child(parent=parent) |
---|
| 0 | + | |
---|
| 0 | + | session = sessionmaker()() |
---|
| 0 | + | session.add(parent) |
---|
| 0 | + | session.add(child1) |
---|
| 0 | + | session.add(child2) |
---|
| 0 | + | session.commit() |
---|
| 0 | + | for child in parent.children: |
---|
| 0 | + | print child.id |
---|
| 0 | + | # prints 2,1 like it's supposed to |
---|
| 0 | + | |
---|
| 0 | + | ########### Flask Extension screws up the ordering |
---|
| 0 | + | from flask import Flask |
---|
| 0 | + | from flaskext.sqlalchemy import SQLAlchemy |
---|
| 0 | + | |
---|
| 0 | + | app = Flask(__name__) |
---|
| 0 | + | db = SQLAlchemy(app) |
---|
| 0 | + | |
---|
| 0 | + | class Parent(db.Model): |
---|
| 0 | + | __tablename__ = 'parents' |
---|
| 0 | + | id = db.Column(db.Integer, primary_key=True) |
---|
| 0 | + | |
---|
| 0 | + | class Child(db.Model): |
---|
| 0 | + | __tablename__ = 'children' |
---|
| 0 | + | id = db.Column(db.Integer, primary_key=True) |
---|
| 0 | + | parent_id = db.Column(db.Integer, db.ForeignKey('parents.id')) |
---|
| 0 | + | parent = db.relationship(Parent, backref=db.backref('children', order_by=id.desc())) |
---|
| 0 | + | |
---|
| 0 | + | def __repr__(self): |
---|
| 0 | + | return "<Child id=%s>" % self.id |
---|
| 0 | + | |
---|
| 0 | + | db.create_all() |
---|
| 0 | + | parent = Parent() |
---|
| 0 | + | child1 = Child(parent=parent) |
---|
| 0 | + | child2 = Child(parent=parent) |
---|
| 0 | + | |
---|
| 0 | + | db.session.add(parent) |
---|
| 0 | + | db.session.add(child1) |
---|
| 0 | + | db.session.add(child2) |
---|
| 0 | + | db.session.commit() |
---|
| 0 | + | for child in parent.children: |
---|
| 0 | + | print child.id |
---|
| 0 | + | # Prints 1,2 despite my order_by=id.desc() in this relationship |
---|
... | |
---|