Skip to content

Instantly share code, notes, and snippets.

@tell-k
Created September 14, 2012 06:46
Show Gist options
  • Save tell-k/3720274 to your computer and use it in GitHub Desktop.
Save tell-k/3720274 to your computer and use it in GitHub Desktop.
class Entry(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
text = Column(String(200))
created_at = Column(DateTime, default=datetime.now, nullable=False)
comments = relationship('Comment', backref="entry") # Commentテーブルへのリレーション
class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
entry_id = Column(Integer, ForeignKey('entries.id'), nullable=False) # ForeingKey
text = Column(String(200))
created_at = Column(DateTime, default=datetime.now, nullable=False)
entry = Entry.query.get(1)
entry.comments[0] # => <__main__.Comment object at 0x10184acd0>
comment = Comment.query.get(1)
comment.entry # => <__main__.Entry object at 0x101841a10> backref設定する事で逆参照できる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment