I use a mixin class to handle this stuff. Example below.

> So, my question: is it generally better practice to name every column 
that you want to pull, even if it's a long list? 
Not really.  There is a "bundle" api here that might be better for you- 
https://docs.sqlalchemy.org/en/13/orm/loading_columns.html

> Also, why does using just RecoveryLogEntries instead of naming each 
column yield a different result?
One is querying a "table", the other is querying a list of items. The 
queried items could be a "table", "table column", "subquery column" or 
several other things.

> It seems weird because in the SQL world, I could do a "SELECT *" or 
"SELECT id, ..." and the output is still in the same format regardless of 
whether I explicitly name name each column or use * to select all columns.
A lot of people approach SqlAclhemy like they are writing SQL. IMHO, a 
better approach is to remember that SqlAlchemy lets you write Python that 
generates Sql for multiple dialects.

> It just seems like it's a whole bunch of typing which could be 
error-prone.  I'll do it if I need to, but what I'm really asking is what 
is the most appropriate/accepted/standard way to do this.

The more standard ways are to expect/inspect the types of objects that are 
queried and act upon it.  The results are not text, but objects. If you 
inspect them, you can pull out the relevant information.

anyways, using a generic mixin (far below), I use the following code.  I 
also sometimes have methods on my objects to return json that only has 
specific fields (such as `as_json_v1`, `as_json_v2`, etc)


class RecoveryLogEntries(Base, UtilityObjectMixin):
    # ... your class ...

# then...

results = session.query(RecoveryLogEntries).limit(record_count)
as_json = [r.loaded_columns_as_dict for r in results]



class UtilityObjectMixin(object):
 """see 
https://github.com/jvanasco/pyramid_sqlassist/blob/master/pyramid_sqlassist/objects.py#L127-L165""";


    def columns_as_dict(self):
        """
        Beware- this function will trigger a load of attributes if they 
have not been loaded yet.
        """
        return dict((col.name, getattr(self, col.name))
                    for col
                    in sa_class_mapper(self.__class__).mapped_table.c
                    )


    def loaded_columns_as_dict(self):
        """
        This function will only return the loaded columns as a dict.
        See Also: ``loaded_columns_as_list``
        """
        _dict = self.__dict__
        return {col.name: _dict[col.name]
                for col in sa_class_mapper(self.__class__).mapped_table.c
                if col.name in _dict
                }


    def loaded_columns_as_list(self, with_values=False):
        """
        This function will only return the loaded columns as a list.
        By default this returns a list of the keys(columns) only.
        Passing in the argument `with_values=True` will return a list of 
key(column)/value tuples, which could be blessed into a dict.
        See Also: ``loaded_columns_as_dict``
        """
        _dict = self.__dict__
        if with_values:
            return [(col.name, _dict[col.name], )
                    for col in sa_class_mapper(self.__class__).mapped_table.
c
                    if col.name in _dict
                    ]
        return [col.name
                for col in sa_class_mapper(self.__class__).mapped_table.c
                if col.name in _dict
                ]

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/874a7299-9fa9-4be9-bb54-3aa186180269o%40googlegroups.com.

Reply via email to