5

I'm working with the SQLAlchemy Expression Language (not the ORM), and I'm trying to figure out how to update a query result.

I've discovered that RowProxy objects don't support assignment, throwing an AttributeError instead:

# Get a row from the table
row = engine.execute(mytable.select().limit(1)).fetchone()

# Check that `foo` exists on the row
assert row.foo is None

# Try to update `foo`
row.foo = "bar"

AttributeError: 'RowProxy' object has no attribute 'foo'

I've found this solution, which makes use of the ORM, but I'm specifically looking to use the Expression Language.

I've also found this solution, which converts the row to a dict and updates the dict, but that seems like a hacky workaround.

So I have a few questions:

  • Is this in fact the only way to do it?
  • Moreover, is this the recommended way to do it?
  • And lastly, the lack of documentation made me wonder: am I just misusing SQLAlchemy by trying to do this?
Community
  • 1
  • 1
Carolyn Conway
  • 1,356
  • 1
  • 15
  • 21
  • What are you trying to accomplish by updating a `RowProxy`? – univerio May 01 '17 at 17:59
  • I want to fetch a row from the database, then I have some python logic that chooses how to update it. Then I just want to update the row I fetched. – Carolyn Conway May 01 '17 at 18:01
  • 1
    Then you are misusing SQLAlchemy. The usage you've described is the benefit of using an ORM. If you only want to restrict yourself to SQLAlchemy Core, then you need to do `mytable.update().values(foo=bar)`. – univerio May 01 '17 at 18:04
  • Ok, thanks. If you post that as an answer, I'll accept it. – Carolyn Conway May 01 '17 at 18:04
  • One question - is it possible using Declarative mapping -> https://docs.sqlalchemy.org/en/latest/orm/mapping_styles.html ? – garbus Feb 03 '19 at 22:45
  • My problem using this construction is, that in this case object named Mytable has no attribute 'update'. – garbus Feb 03 '19 at 22:56

1 Answers1

4

You are misusing SQLAlchemy. The usage you've described is the benefit of using an ORM. If you only want to restrict yourself to SQLAlchemy Core, then you need to do

engine.execute(mytable.update().where(mytable.c.id == <id>).values(foo="bar"))
univerio
  • 19,548
  • 3
  • 66
  • 68