cosmicpython / code

Example application code for the python architecture book
Other
2.07k stars 926 forks source link

Possible typo with Chapter 5 code sample #4

Closed daredevil82 closed 9 months ago

daredevil82 commented 4 years ago

In Chapter 5, a test for uow is displayed. However, it is using list unpacking, and its not obvious whether the difference between orderlineid and id in

[[orderlineid]] = session.execute(
    'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku',
    dict(orderid=orderid, sku=sku)
)

is deliberate or a typo. If it is deliberate, it would be helpful to have an explanation of what SQLAlchemy's orm.session package is doing to return the thing referred to with the name orderlineid

hjwp commented 4 years ago

it's definitely a bit of a head-scratcher. session.execute returns the results of your SELECT query, which the set of rows that match the query. each row is made up of columns. so the return type is a list of tuples. in this case i know it's exactly one row containing a tuple with exactly one item (the id of the row in the order_lines table), which is why the tuple unpacking works.

i could add a footnote maybe? i didn't want the focus of the book to be teaching people how to use sqlalchemy, but i don't want code listings to be so weird as to be obscure and cause people to wonder how on earth they work!

adrienbrunet commented 4 years ago

I had to read it twice and give it a few seconds of thoughts. A footnote should help !

Also for me, it would be a bit less confusing with

[(orderlineid, )] = ...
hjwp commented 9 months ago

fixed here, thanks for the feedback everyone. https://github.com/cosmicpython/book/commit/609d5a2303c632012bee7105a6d83eb03f4b708a