1

Is it possible to ask query object to retrieve all columns but the Foreign and Primary key?

session_object.query(Table_Class).all(exclude={pk:True,fk=True})

Currently I am manually removing them after I get them back.

Max
  • 4,152
  • 4
  • 36
  • 52
  • I assume you already know you can query each column in table you want, instead of querying the entire table. I just spent a few minutes looking for exactly what you're asking for and can't seem to find it. It might not exist; a way to exclude certain column(s) from the query results. – Andrew Clark Oct 06 '22 at 01:48
  • Indeed, I know how I can ask for each column or set of them further more, I can always get a list of FK and PK and then subtract it from the set of all columns and ask for them. However, given that SA is supposed to make things easier, I thought there might be some nugget of gold somewhere, if no one answers I will post up my own little hack – Max Oct 06 '22 at 01:50
  • Right now are you just popping the keys off, each time? I've had to do this for solutions in the past as well. I'll be interested to see which hack you came up with. I might borrow it in the future when I come across this situation. – Andrew Clark Oct 06 '22 at 01:55
  • On the solution I posted below, I'm making an assumption that I'm understanding the documentation correctly. I haven't actually tried it. It could be except_ only allows you to remove results which differ due to a change in filter, it might not actually let you just remove the columns, I'm not sure. – Andrew Clark Oct 06 '22 at 02:02

1 Answers1

0

The closest thing I found was an 'except_', which would you require you create 3 separate queries. Not sure if this would be worth it for you or not. I'm guessing probably not.

q1 = sess.query(SomeClass.id).all()
q2 = sess.query(SomeClass).all()

q3 = q1.except_(q2)

Source -> https://docs.sqlalchemy.org/en/14/orm/query.html?highlight=except#sqlalchemy.orm.Query.except_

Andrew Clark
  • 850
  • 7
  • 13