Skip to content

Instantly share code, notes, and snippets.

@plq
Created June 29, 2015 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plq/4fb41846700822e343de to your computer and use it in GitHub Desktop.
Save plq/4fb41846700822e343de to your computer and use it in GitHub Desktop.
relationship test
from sqlalchemy import *
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child = relationship("Child", uselist=False, backref="parent")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
Or to turn a one-to-many backref into one-to-one, use the backref() function to provide arguments for the reverse side:
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship("Child", backref=backref("parent", uselist=False))
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment