Last active
August 25, 2017 07:01
-
-
Save necrobuffalo/9d613c6a6b819caa12b728ee1fb09739 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
(sqlalchemy) [11:53:29 PM] azathoth:~/src/misc | |
% python sqlthing.py | |
before adding to session: <Thing(id=None, content=None)> | |
after adding to session: <Thing(id=None, content=None)> | |
after commit: <Thing(id=1, content=pending)> | |
(sqlalchemy) [11:53:32 PM] azathoth:~/src/misc | |
% |
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
from sqlalchemy import create_engine | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import Column, Integer, String | |
from sqlalchemy.orm import sessionmaker | |
Base = declarative_base() | |
class Thing(Base): | |
__tablename__ = "things" | |
id = Column(Integer, primary_key=True) | |
content = Column(String, server_default="pending", default="pending") | |
def __repr__(self): | |
return "<Thing(id={}, content={})>".format(self.id, self.content) | |
engine = create_engine('sqlite:///:memory:', echo=False) | |
Session = sessionmaker(bind=engine) | |
Base.metadata.create_all(engine) | |
db_session = Session() | |
item = Thing() | |
print "before adding to session: " + str(item) | |
db_session.add(item) | |
print "after adding to session: " + str(item) | |
db_session.commit() | |
print "after commit: " + str(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment