Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the data in a join SQLAlchemy

Given the following sess query:

sess = DBSession()  
reg =  sess.query(Registration,Schedule).join(Schedule,Registration.classcode==Schedule.classcode).filter(Registration.registration_no==reg_id)

How the heck do I address the fields in the result 'reg'?

reg.timeofentry throws an error: TypeError: Can't convert 'int' object to str implicitly

I am rapidly learning to loath SQLAlchemy for all it's cryptic wrappers around perfectly simply SQL syntaxes.

like image 470
Erik Avatar asked Mar 04 '26 23:03

Erik


1 Answers

oddly enough, the names within the named tuple row as returned from iterating the result reg in this case would be be Registration and Schedule:

for row in reg:
   print row.Registration, row.Schedule

I'm not able to reproduce your specific error message "can't convert 'int' object to str implicitly". If I attempt to access a name on the KeyedTuple that doesn't exist, there's a clear message:

AttributeError: 'KeyedTuple' object has no attribute 'imfake'

The behavior of the return value of query should be less cryptic after reading the intro to Querying in the ORM tutorial.

like image 195
zzzeek Avatar answered Mar 07 '26 11:03

zzzeek



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!