On Wed, Dec 20, 2017 at 11:25 PM, Tom Tanner
<dontsendemailher...@gmail.com> wrote:
> I moved `db.session.commit()` outside the loop. That seemed to fix the
> error. I'll post an update if I get more errors.

sure, committing outside the loop is definitely better.   Still I'd
need to play with it to see why the second object in the list wouldn't
be persistent anymore, it would only be expired and should re-load its
".id" attribute.


>
> On Wednesday, December 20, 2017 at 6:43:05 PM UTC-5, Lele Gaifax wrote:
>>
>> Tom Tanner <dontsende...@gmail.com> writes:
>>
>> > I want to query rows and copy them while changing an attribute of each.
>> > Here's my code.
>> >
>> >  colObjs= db.session.query(Column).filter_by(chart_id=oldChartID).all()
>> >  for colObj in colObjs:
>> >    make_transient(colObj)
>> >    print colObj.id
>> >    del colObj.id
>> >    colObj.chart_id= newChartID
>> >    db.session.add(colObj)
>> >    db.session.commit()
>> >
>> > In this example, `colObjs` has two objects.
>>
>> Not sure about what causes that, but maybe you just need to commit once,
>> at
>> the end of the loop, that is something like
>>
>>     colObjs= db.session.query(Column).filter_by(chart_id=oldChartID).all()
>>     for colObj in colObjs:
>>         make_transient(colObj)
>>         print colObj.id
>>         del colObj.id
>>         colObj.chart_id= newChartID
>>         db.session.add(colObj)
>>     # Single commit, when the loop above exits
>>     db.session.commit()
>>
>> ciao, lele.
>> --
>> nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
>> real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
>> le...@metapensiero.it  |                 -- Fortunato Depero, 1929.
>>
> --
> 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.

-- 
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