On Fri, Feb 9, 2018 at 6:45 AM, Jeremy Flowers
<jeremy.g.flow...@gmail.com> wrote:
> Also this didn't work for me:
>
> print(session.query(Jobmst)
>              .values(Jobmst.jobmst_type,
>                      Jobmst.jobmst_name)
>              .first()
>       )
>
> yields:
> AttributeError: 'generator' object has no attribute 'first'

yes, as mentioned earlier, "values()" is a weird method from earlier
days, it produces an iterator directly:

for row in query.values(a, b, c):
   # etc

with_entities() is more modern and fits into the usual model where you
get a Query you can all all() upon


>
> Swapping first to earlier in the chain like so:
>
> print(session.query(Jobmst).first()
>              .values(Jobmst.jobmst_type,
>                      Jobmst.jobmst_name)
>       )
>
> yields:
>
>
> AttributeError: 'Jobmst' object has no attribute 'values'

OK the confusion here is being aware of what kinds of objects
different methods return.   The ORM tutorial is a good place to start
to get a feel for it.   The first() / one() / and all() methods
execute the SQL query and return you a result - when you call those,
you no longer have a Query object, you've got your data back.   The
values() method, which doesn't fit into any of this very well, will
execute the SQL query and also return you a result, but it returns it
as an iterator, but still, when you call it, you no longer have a
Query object.    If you used the with_entities() method, when you're
done calling that, you still have a Query object and you can still do
more things with it.


>
> I get confused as well, thinking that *columns (from here) infers a list of
> some kind.
>
> So if I don't have a columns variable assigned to a list, I should be able
> to embed one directly with this sort of syntax values([c1,c2])
> But you indicate something like values(c1,c2).
> So that confuses me as a newbie to Python (my background being predominantly
> Java, Groovy, Typescript, Javascript, Angular 2-4, Ionic 2-3).
> The * prefix immediately clicked for my from somewhere else. Maybe
> academically from reading about C++.. Something like a pointer to the whole
> array.

My impression is that yes the "*" was inspired by C notation like you
see for printf, but I'm sure the historians would know more about
that.  Google keeps returning this as the top hit for "python variadic
arguments": 
https://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/



>
> I've been brushing up on things like *args *kwargs too - so understand their
> purpose.
> Duck typing, passing parameters as a dictionary is known to me from Groovy.
> It still confounds me though knowing what type to pass in the values().
> Have been reading here about it:
>
>
>
>
> On Friday, 9 February 2018 09:48:51 UTC, Jeremy Flowers wrote:
>>
>> I was thinking about this overnight..
>> Do values relates specifically to inserts and updates, not selects/deletes
>> perhaps, which would correlate to SQL syntax.
>> If, it would make sense to indicate that in the documentation
>>
>> On Thursday, 8 February 2018 20:29:45 UTC, Jeremy Flowers wrote:
>>>
>>> I've seen you can do things like this:
>>> fields = ['jobmst_type', 'jobmst_name']
>>>  print(session.query(Jobmst).options(load_only(*fields)).first())
>>>
>>> But according to the documentation, you should be able to do something
>>> with Query values() too.
>>> But once again I seem to be dumbfounded by the syntatic sugar.
>>>
>>> What data type does the list of values need?
>>> Is there a way to introspect that ahead of time?
>>>
>>> Thought I was onto something with .base_columns, but that didn't work
>>> either...
>>>
>>> I ended up with something like an instrumentalAttributes mismatch.
>>> print(session.query(Jobmst)
>>>              .values([Jobmst.jobmst_type.base_columns,
>>>                       Jobmst.jobmst_name.base_columns
>>>                       ])
>>>              .first()
>>>       )
>>>
>>> sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped
>>> entity expected - got '[{Column('jobmst_type', Numeric(scale=0,
>>> asdecimal=False), table=<jobmst>, nullable=False)}, {Column('jobmst_name',
>>> String(length=256), table=<jobmst>, nullable=False)}]'
>>>
>>> BTW: I'm aware of querying with things like query(Jobmst.jobmst_type,
>>> Jobmst.jobmst_name) too - but looking to understand what values expects.
>>> Mike, Can documentation not specify type?
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to