4 ms·
If you had a data structure with your fields, you could do that much more explicitly. Not a Ruby guy but the Ruby equivalent of something like: rejected [:
by _asummers 8y ago
If you had a data structure with your fields, you could do that much more explicitly.
Not a Ruby guy but the Ruby equivalent of something like:
rejected [:updated_at, :inserted_at]
to_select =
Enum.reject(fields, fn field ->
field in rejected
end)
- anderse 8y agoProbably something like: to_select = fields - [:created_at, :updated_at] Though there’s always a slight uncomfort in not being 100% sure the array elements aren’t strings, so might throw in a cast here just to be sure.
- blattimwind 8y agoIn SQLAlchemy it could look something like this cols = list(table.columns) cols.remove(table.columns['updated_at']) cols.remove(table.columns['inserted_at']) query = select(cols).select_from(table) (You can abbreviate table.columns with table.c) With sqlalchemy.orm you could also define a Bundle, see https://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=bundle#sqlalchemy.orm.query.Bundle https://docs.sqlalchemy.org/en/latest/orm/query.html?highlig...
- djd20 8y agoMuch simpler usint the ORM - you can defer columns: http://docs.sqlalchemy.org/en/latest/orm/loading_columns.html#deferred-column-loading http://docs.sqlalchemy.org/en/latest/orm/loading_columns.htm... This is both at the 'object' level, and at the query level for example at the query level: from sqlalchemy.orm import defer, undefer query = session.query(Book) query = query.options(defer('summary')) query = query.options(undefer('excerpt')) query.all() You can also group columns for deferral/undeferral. I use this loads.
- blattimwind 8y agoInteresting, I knew about defer and used it in the past for pretty much the purpose the first example illustrates, but didn't remember/realize it could be used on a per-query basis. I think if you strip columns for performance reasons for one specific query it would still be better to use Bundles or the equivalent loose select (i.e. session.query(Book.id, Book.title)) instead of query-local-deferred columns, because then you will notice if a code change leads to a down-stream access of an unwanted column for some reason.
- Bishonen88 8y agowhat sql does this produce though? I can imagine that there'd be a hit on performance
- blattimwind 8y agoWhy would you imagine that? This compiles to SELECT col1,...,coln FROM table ...;