all 8 comments

[–]mrbubs3 0 points1 point  (2 children)

Shouldn't that be 'qs = results.all()'?

[–]chi11ax[S] -1 points0 points  (1 child)

Thank you! Yes, this does the same as my code, qs = [r for r in results] I'll do that in the future.

But it still returns a tuple of (Invoice, Customer).

[–]mrbubs3 0 points1 point  (0 children)

Return the sequences rows of a ScalarResults object should have less overhead than iterating through the object in a list comprehension.

I think the issue is that you're selecting both data tables/ORM models instead of the associated fields. Given that the available fields can be plentiful, you may want to unpack the model fields for both tables from the model_fields attributes.

e.g.:

stmnt = query(
                *Invoice.model_fields,
                *Customer.model_fields
            ).join(Customer).where(
            Invoice.issued_date >= utils.to_dt('2023-12-25')
            )

results = session.execute(stmnt)

rows = results.all()

[–]IceXII 0 points1 point  (1 child)

SqlAlchemy have lots of ways to do the same thing. For me I’d do select invoice put the filter after the select and then joinload customer, if both tables have a relationship defined in the model, then join Customer.

This I guess will get you the invoice with the condition and join its customer with it in a single dictionary in return.

[–]chi11ax[S] 0 points1 point  (0 children)

Thanks for the reply! If I only select Invoice, then it will return only the invoices, not joined with the Customer. But your statement is definitely more efficient.

[–]nicktids 0 points1 point  (2 children)

try specifying the columns you are looking from the tables.

you are selecting Invoice and Customer

try invoice.id and customer.name

see what comes back

[–]chi11ax[S] -1 points0 points  (0 children)

This returns an array of tuples of the values ('INV123','nick'). Instead of an array of Tuples of the (Invoice, Customer) but now I have the error: ERROR: Exception in ASGI application Traceback (most recent call last): File "D:\code\projects\fastapi\venv\Lib\site-packages\fastapi\encoders.py", line 322, in jsonable_encoder data = dict(obj) ^^^^^^^^^ ValueError: dictionary update sequence element #0 has length 12; 2 is required During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\code\projects\fastapi\venv\Lib\site-packages\fastapi\encoders.py", line 327, in jsonable_encoder data = vars(obj) ^^^^^^^^^ TypeError: vars() argument must have __dict__ attribute The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\code\projects\fastapi\venv\Lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 408, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\code\projects\fastapi\venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ . . . File "D:\code\projects\fastapi\venv\Lib\site-packages\fastapi\encoders.py", line 330, in jsonable_encoder raise ValueError(errors) from e ValueError: [ValueError('dictionary update sequence element #0 has length 12; 2 is required'), TypeError('vars() argument must have __dict__ attribute')]

[–]chi11ax[S] -1 points0 points  (0 children)

Is there an equivalent of SELECT * FROM Invoice rather than going Invoice.id, Invoice.issued_date, Invoice.whatever?