On Wed, Dec 20, 2017 at 5:38 PM, Tom Tanner <[email protected]> wrote: > 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()
is that the actual query? Are you perhaps getting back the same colObj twice? it looks like the two rows are the same object somehow but that query above wouldn't have that effect. Other queries could have this effect however. can you show full program output please and also set echo='debug' on create_engine and show that as well? thanks > > In this example, `colObjs` has two objects. I loop thru `colObjs`, going > over each `colObj`. The first item in the loop copies fine. But when I try > copying the second one, I get this error. > Traceback (most recent call last): > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1997, > in __call__ > return self.wsgi_app(environ, start_response) > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1985, > in wsgi_app > response = self.handle_exception(e) > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1540, > in handle_exception > reraise(exc_type, exc_value, tb) > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1982, > in wsgi_app > response = self.full_dispatch_request() > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1614, > in full_dispatch_request > rv = self.handle_user_exception(e) > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1517, > in handle_user_exception > reraise(exc_type, exc_value, tb) > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1612, > in full_dispatch_request > rv = self.dispatch_request() > File "/path/to/local/lib/python2.7/site-packages/flask/app.py", line 1598, > in dispatch_request > return self.view_functions[rule.endpoint](**req.view_args) > File "/path/to/myApp.py", line 859, in copyGraphic > del colObj.id > File > "/path/to/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", > line 227, in __delete__ > self.impl.delete(instance_state(instance), instance_dict(instance)) > File > "/path/to/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", > line 679, in delete > del dict_[self.key] > KeyError: 'id' > > Furthermore, the output of the `print` statement on the first item of the > loop shows the `coloObj` id, but the second one outputs `None`. > > Why does this error happen? How do I fix it? > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
