On Tue, Aug 22, 2017 at 9:26 AM, Konstantin Kashin <kvkas...@gmail.com> wrote:
> # Setup
> Suppose I have a table with two fields: a string primary key and a boolean
> flag. I want to query multiple rows and then update the flag across all of
> them, then commit my changes. I have a MySQL DB with the following DBAPI
> (http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb-module.html).
>
> ## Model
> class TestSQLTable(Base):
>     __tablename__ = 'test_table'
>
>
>     some_string_id = Column(String(32), nullable=False, primary_key=True)
>     flag = Column(Boolean, nullable=False, default=True)
>
>
> # SQLAlchemy 1.0.12
>
> In SQLAlchemy 1.0.12, the following worked:
>
>
>     sm = sessionmaker(
>         autocommit=False,
>         autoflush=True,
>         expire_on_commit=True,
>         bind=engine,  # MySQL engine
>     )
>     session_factory = scoped_session(sm)
>
>
>     def modify_records(session, flag=False):
>         records = session.query(TestSQLTable).all()
>         for r in records:
>             r.flag = flag
>
>
>     session = session_factory()
>     modify_records(session)
>     session.commit()
>
>
>
> # SQLAlchemy 1.1.13
> Now, this does *not* work and causes a core dump. I am unable to tell what
> change was made in 1.1 that causes this different behavior when reading the
> ["What's New in SQLAlchemy
> 1.1?"](http://docs.sqlalchemy.org/en/latest/changelog/migration_11.html)
> guide.
>
> If I do this in a Jupyter notebook, for instance, the kernel just crashes on
> commit without any error.
>
> However, this **does** work for just a single row:
>
>     def modify_record(session, flag=False):
>         record = session.query(TestSQLTable).first()
>         record.flag = flag
>
>
> ## Ways I have gotten this to work
>
> ### Add an explicit return
>
>     def modify_records(session, flag=False):
>         records = session.query(TestSQLTable).all()
>         for r in records:
>             r.flag = flag
>         return records  # ADD THIS!
>
>
> ### Flush after each mutation
>
>
>     def modify_records(session, flag=False):
>         records = session.query(TestSQLTable).all()
>         for r in records:
>             r.flag = flag
>             session.flush()  # ADD THIS!
>
>
> ### Restructure code so that the commit happens inside `modify_records`
>
> This works, but is not the way the application is currently built and would
> thus require a major refactor (b/c commit happens via a decorator).
>
> # Main question
>
> Why does this happen exactly for multiple records and why did this work in
> 1.0 but does not in 1.1?

What platform are you running this on, and how have you installed
MySQLdb and SQLAlchemy? If you ugraded SQLAlchemy in-place, is there
any chance that you are running a mix of old and new code? Have you
reproduced the error in a completely fresh installation?

Simon

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to