Skip to content

Instantly share code, notes, and snippets.

@tell-k
Created September 13, 2012 10:34
Show Gist options
  • Save tell-k/3713469 to your computer and use it in GitHub Desktop.
Save tell-k/3713469 to your computer and use it in GitHub Desktop.
# テーブルを表現するオブジェクト
entry_table = Table(
'entries', metadata,
Column("id", Integer, primary_key=True),
Column("text", String(200)),
Column("created_at", DateTime, default=datetime.now, nullable=False)
)
# Tableオブジェクトとマッピングされるクラス
class Entry(object):
def __init__(self, id, text, created_at=None):
self.id = id
self.text = text
self.created_at = created_at
Entry.metadata = metadata
Entry.query= db_session.query_property()
# EntryクラスとTableオブジェクトをマッピング
mapper(Entry, entry_table)
Entry.metadata.create_all() # テーブル作成
entry = Entry(id=1, text="entrytext") # オブジェクト生成
db_session.add(entry) # DBにインサート
db_session.commit()
entry = Entry.query.first() # データの取得
print entry.text # => "entrytext"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment