Skip to content

Instantly share code, notes, and snippets.

@ods
Last active August 29, 2015 14:06
Show Gist options
  • Save ods/4459e5e630ccec2ba6b3 to your computer and use it in GitHub Desktop.
Save ods/4459e5e630ccec2ba6b3 to your computer and use it in GitHub Desktop.
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Link(Base):
__tablename__ = 'Link'
id = Column(Integer, primary_key=True)
doc_id = Column(Integer, ForeignKey('Doc.id'), nullable=False)
order = Column(Integer, nullable=False, default=0)
def __repr__(self):
return 'Link(id={})'.format(self.id)
class Doc(Base):
__tablename__ = 'Doc'
id = Column(Integer, primary_key=True, autoincrement=True)
links = relationship(Link, order_by=[Link.order])
a2 = Link(id=2)
doc = Doc()
doc.links = [Link(id=1), a2]
old_links = doc.links
print old_links # [Link(id=1), Link(id=2)]
doc.links = [Link(id=3), a2]
print old_links # [Link(id=2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment