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
# 某フレームワークで OR を表現 | |
Entry.objects.get(Q(id=1) | Q(id=2)) | |
# => SELECT * FROM etnries WHERE id = 1 OR id = 2 |
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
db_session = scoped_session(sessionmaker(bind=engine)) |
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") | |
class Comment(Base): | |
__tablename__ = 'comments' |
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
same object | |
entry1 => <__main__.Entry object at 0x1018408d0> | |
entry1_another => <__main__.Entry object at 0x1018408d0> |
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
# 異なる方法で同じレコードを取り出してみる | |
entry1 = Entry.query.filter_by(id=1).first() | |
entry1_another = Entry.query.filter_by(text="entrytext1").first() | |
# IdentityMapにより同じオブジェクトが返ってくる | |
if entry1 == entry1_another: | |
print "same object" | |
print "entry1 => %s" % (entry1) | |
print "entry1_another => %s" % (entry1_another) |
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
INSERT INTO entries (id, text, created_at) VALUES (%s, %s, %s) | |
(1, 'entrytext', datetime.datetime(2012, 9, 13, 23, 21, 11, 165187)) | |
# Session は ここで selectされる事を知ってるから↑で先にInser文をちゃんとDBに流してくれている。 | |
SELECT entries.id AS entries_id, entries.text AS entries_text, entries.created_at AS entries_created_at | |
FROM entries | |
WHERE entries.id = %s | |
LIMIT %s | |
INSERT INTO entries (id, text, created_at) VALUES (%s, %s, %s) |
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
db_session = scoped_session(sessionmaker(bind=engine)) | |
db_session.add(Entry(id=1, text="entrytext")) | |
# select entry ------ | |
entry1 = Entry.query.filter_by(id=1).first() | |
print entry1.text # => "entrytext" | |
db_session.add(Entry(id=2, text="entrytext")) | |
db_session.flush() |
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
# Batch Insert | |
INSERT INTO entries (id, text, created_at) VALUES (%s, %s, %s) | |
((1, 'entrytext', datetime.datetime(2012, 9, 13, 23, 8, 38, 370579)), (2, 'entrytext', datetime.datetime(2012, 9, 13, 23, 8, 38, 370587))) |
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
db_session = scoped_session(sessionmaker(bind=engine)) | |
db_session.add(Entry(id=1, text="entrytext")) # <= Insert | |
db_session.add(Entry(id=2, text="entrytext")) # <= Insert | |
db_session.flush() # <= このタイミングでDBに反映 |
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
Base = declarative_base() # <= これ | |
Base.query = db_session.query_property() | |
Base.metadata = metadata | |
# 宣言的にテーブルを定義 | |
class Entry(Base): | |
__tablename__ = 'entries' | |
id = Column(Integer, primary_key=True) | |
text = Column(String(200)) | |
created_at = Column(DateTime, default=datetime.now, nullable=False) |