Last active
January 22, 2017 17:29
-
-
Save porterjamesj/647b3c4c521c77ee77bdacf282effa82 to your computer and use it in GitHub Desktop.
Demonstration that SQLAlchemy doesn't facilitate the first bug described in http://kevinmahoney.co.uk/articles/django-orm-mistakes/
This file contains hidden or 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.ext.declarative import declarative_base | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy import Column, Integer, create_engine | |
Base = declarative_base() | |
class Thing(Base): | |
__tablename__ = "thing" | |
id = Column(Integer, primary_key=True) | |
foo = Column(Integer) | |
bar = Column(Integer) | |
engine = create_engine('sqlite:///:memory:') | |
engine.echo = True | |
Base.metadata.create_all(engine) | |
Session = sessionmaker(bind=engine) | |
def create(): | |
session = Session() | |
thing = Thing(foo=1, bar=1) | |
session.add(thing) | |
session.commit() | |
set_foo(thing.id) | |
thing.bar = 2 | |
session.commit() | |
def set_foo(id): | |
session = Session() | |
thing = session.query(Thing).get(id) | |
thing.foo = 2 | |
session.commit() | |
create() | |
session = Session() | |
thing = session.query(Thing).one() | |
print thing.foo, thing.bar # 2, 2 |
This file contains hidden or 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
2017-01-22 12:21:06,802 INFO sqlalchemy.engine.base.Engine () | |
2017-01-22 12:21:06,803 INFO sqlalchemy.engine.base.Engine COMMIT | |
2017-01-22 12:21:06,804 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) | |
2017-01-22 12:21:06,805 INFO sqlalchemy.engine.base.Engine INSERT INTO thing (foo, bar) VALUES (?, ?) | |
2017-01-22 12:21:06,805 INFO sqlalchemy.engine.base.Engine (1, 1) | |
2017-01-22 12:21:06,805 INFO sqlalchemy.engine.base.Engine COMMIT | |
2017-01-22 12:21:06,806 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) | |
2017-01-22 12:21:06,807 INFO sqlalchemy.engine.base.Engine SELECT thing.id AS thing_id, thing.foo AS thing_foo, thing.bar AS thing_bar | |
FROM thing | |
WHERE thing.id = ? | |
2017-01-22 12:21:06,807 INFO sqlalchemy.engine.base.Engine (1,) | |
2017-01-22 12:21:06,808 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) | |
2017-01-22 12:21:06,809 INFO sqlalchemy.engine.base.Engine SELECT thing.id AS thing_id, thing.foo AS thing_foo, thing.bar AS thing_bar | |
FROM thing | |
WHERE thing.id = ? | |
2017-01-22 12:21:06,809 INFO sqlalchemy.engine.base.Engine (1,) | |
2017-01-22 12:21:06,810 INFO sqlalchemy.engine.base.Engine UPDATE thing SET foo=? WHERE thing.id = ? | |
2017-01-22 12:21:06,810 INFO sqlalchemy.engine.base.Engine (2, 1) | |
2017-01-22 12:21:06,810 INFO sqlalchemy.engine.base.Engine COMMIT | |
2017-01-22 12:21:06,811 INFO sqlalchemy.engine.base.Engine UPDATE thing SET bar=? WHERE thing.id = ? | |
2017-01-22 12:21:06,811 INFO sqlalchemy.engine.base.Engine (2, 1) | |
2017-01-22 12:21:06,811 INFO sqlalchemy.engine.base.Engine COMMIT | |
2017-01-22 12:21:06,812 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) | |
2017-01-22 12:21:06,813 INFO sqlalchemy.engine.base.Engine SELECT thing.id AS thing_id, thing.foo AS thing_foo, thing.bar AS thing_bar | |
FROM thing | |
2017-01-22 12:21:06,813 INFO sqlalchemy.engine.base.Engine () | |
2 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment