Skip to content

Instantly share code, notes, and snippets.

@kmarekspartz
Created August 23, 2013 21:19
Show Gist options
  • Save kmarekspartz/6324142 to your computer and use it in GitHub Desktop.
Save kmarekspartz/6324142 to your computer and use it in GitHub Desktop.
SQLAlchemy OrderingList misbehavior?
from sqlalchemy import Column, Integer, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.orm import relationship, backref, sessionmaker
Base = declarative_base()
class Slide(Base):
__tablename__ = 'slide'
id = Column(Integer, primary_key=True)
class Bullet(Base):
__tablename__ = 'bullet'
id = Column(Integer, primary_key=True)
slide_id = Column(Integer, ForeignKey('slide.id'))
position = Column(Integer)
slide = relationship(
"Slide",
backref=backref(
"bullets",
order_by="Bullet.position",
collection_class=ordering_list('position')))
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
s = Slide()
session.add(s)
session.commit()
session.close()
session = Session()
b = Bullet()
b.slide = s
session.add(b)
session.commit()
session.refresh(b)
session.close()
assert b.position is None # But I expect it to not be None
session = Session()
s = Slide()
session.add(s)
session.commit()
session.refresh(s)
s_id = s.id
session.close()
del s
session = Session()
b = Bullet()
b.slide_id = s_id
session.add(b)
session.commit()
session.refresh(b)
session.close()
assert b.position is None # But I expect it to not be None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment