views:

10

answers:

0

Hello.

I have simple problem. In my table have columns ID, NAME, CONTENT, TIMESTAMP. If i use session.query(table).all() result is array of table class objects. So i have no problem with modify one or more objects and update, or use this objects in associations. But i need only columns ID and NAME. If use session.query(table.id, table.name) i get as result tuple with id and name. Using this tuple in update it's - maybe - easy, but code it's.. ugly. As same in associations. And there's is unnecessary database load if i want only ID to make association.

Until now i use simple code to get array of table class objects querying only specified columns:
for row in session.query(table.id, table.name).all():
data.append(table(id=row.id, name=row.name))

It's make updates easy, but using in associations is complex. For example, i get 10 rows in array of table objects from my code. I want association 1 row to table2.
If i use: session.add(table2(name = 'test', association = data[1])) and commit,
sqlalchemy want to create new table2 row, new association table row and new table row (this first, associated to table2).

It's there any way to get result as array of table objects, not tuples, with specified columns, wheres returning objects is same if make query(table)?

Sorry for my English anyway, i never used it to describe complex problem, until now.