[sqlalchemy] Superfluous SELECT when reusing object after commit

16 views
Skip to first unread message

Julian Scheid

unread,
May 17, 2010, 6:20:44 AM5/17/10
to sqlal...@googlegroups.com
Is there a way to avoid the SELECT statement issued by the following
test case, i.e. to have some_a "survive" the commit?

--- 8< ---

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relation
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine("sqlite://", echo=True)
session = sessionmaker(bind=engine)()

Base = declarative_base()

class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)

class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey('a.id'), nullable = False)
a = relation(A)

Base.metadata.create_all(bind=engine)

some_a = A()
session.add(some_a)
session.commit()

some_b = B(a=some_a)
session.add(some_b)
session.commit()

--- >8 ---

# Table creation etc omitted

2010-05-17 22:12:06,533 INFO sqlalchemy.engine.base.Engine.0x...41d0 BEGIN
2010-05-17 22:12:06,534 INFO sqlalchemy.engine.base.Engine.0x...41d0
INSERT INTO a DEFAULT VALUES
2010-05-17 22:12:06,534 INFO sqlalchemy.engine.base.Engine.0x...41d0 []
2010-05-17 22:12:06,534 INFO sqlalchemy.engine.base.Engine.0x...41d0 COMMIT
2010-05-17 22:12:06,535 INFO sqlalchemy.engine.base.Engine.0x...41d0 BEGIN
2010-05-17 22:12:06,536 INFO sqlalchemy.engine.base.Engine.0x...41d0
SELECT a.id AS a_id
FROM a
WHERE a.id = ?
# <-- I'd like to avoid this
2010-05-17 22:12:06,536 INFO sqlalchemy.engine.base.Engine.0x...41d0 [1]
2010-05-17 22:12:06,537 INFO sqlalchemy.engine.base.Engine.0x...41d0
INSERT INTO b (a_id) VALUES (?)
2010-05-17 22:12:06,537 INFO sqlalchemy.engine.base.Engine.0x...41d0 [1]
2010-05-17 22:12:06,537 INFO sqlalchemy.engine.base.Engine.0x...41d0 COMMIT

--- 8< ---

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Michael Bayer

unread,
May 17, 2010, 9:51:22 AM5/17/10
to sqlal...@googlegroups.com
set expire_on_commit=False on your sessionmaker.

Julian Scheid

unread,
May 18, 2010, 1:57:09 AM5/18/10
to sqlal...@googlegroups.com
Thanks, sorry must have missed that in the docs.
Reply all
Reply to author
Forward
0 new messages