Created
September 14, 2012 06:46
-
-
Save tell-k/3720274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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