Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sqlAlchemy bulk update but non-serializable JSON attributes

I am trying to better understand how I can bulk update rows in sqlAlchemy using a Python function for each row that requires dumping results to json without having to iterate over them individually:

def do_something(x):
  return x.id + x.offset
table.update({Table.updated_field: do_something(Table)})

This is a simplification of what I am trying to accomplish except I get the error TypeError: Object of type InstrumentedAttribute is not JSON serializable.

Any thoughts on how to fix the issue here?

like image 564
DarkDrassher34 Avatar asked Sep 15 '26 13:09

DarkDrassher34


1 Answers

Why are you casting your Table Id to a json String? remove it and try.

Edit:

You can't call the same object in bulk, you can for example:

table.update({Table.updated_field: json.dumps(object_of_my_table variable._asdict())})

If you want update your column attribute with the whole object you will must loop and dump it in the update as:

for table in dbsession.query(Table):
    table.update_field = json.dumps(table._asdict())
    dbsession.add(table).
like image 145
Ektorr Avatar answered Sep 17 '26 04:09

Ektorr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!