sqlalchemy.orm.subquery does not seem to load only specific columns - sqlalchemy

how can i load only specific columns when using subquery?
It seems like subquery loads all the columns from the table even though I included load_only option before calling subquery.
Code snippet is
results = session.query(User).options(load_only(User.name, User.fullname))
results = results.subquery()
The first result statement only loads name and fullname from User but the second result statement loads all the columns.
Any help is greatly appreciated. Thanks so much.

I just found a solution. It works if i do
results = session.query(User.name, User.fullname)
results = results.subquery()
Not sure if anynone has a better solution?

Related

Result set must be in groupings - MYSQL

I am making a report using java code and it is working fine (Image Right).
Now I want to convert it to BIRT.
Since BIRT focuses on Query, I have to change my java code to plain query. Problem is, I have a for each loop on java query where mysql doesn't have.
I found similar query in here but it did not work on me.
As of now, using plain query this is my result:
BIRT RESULT
What am I missing in here? Here's my code.
SELECT item.No_, item.Description, item_ledger_entry.Item_No_,
item_ledger_entry.Description,item_ledger_entry.Posting_Date,
item_ledger_entry.External_Document_No_, item_ledger_entry.Document_No_,
item_ledger_entry.Location_Code, item_ledger_entry.Quantity,
item_ledger_entry.Entry_Type
FROM pbsdev3.item, pbsdev3.item_ledger_entry
where item.No_ = item_ledger_entry.Item_No_
and item.Description = item_ledger_entry.Description
group by item.No_;
I am a very fresh coder so I don't have much knowledge yet.
The result I wanted Look like this(It must be this)
This is the item Table and the item_ledger_entry
Help much appreciated!
Actually, your query is good working good. Make sure you are using Table instead of grid. Table is better than grid when it comes to multiple data.
And by the way, you don't need that groupings.

like and not like in access query

I want user to be able to filter results for a query to either INclude or EXclude a string based on value in a checkbox.
The Checkbox is referenced correctly and when checked does indeed filter just on Hackney, however when unchecked the "not" part gives (wrongly) zero records
IIf([Forms]![Navigation]![TEST]=True,"Hackney",Not In ("Hackney"))
Any suggestions very welcome, been trying different options all morning.
You can not do what you are trying to do. You are trying to build your SQL query through this IIf statement.
Based on what I believe you want, what would work in your case is the following:
select ... /* your select fields */
from .... /* your table(s) */
where .... /* the rest of your conditions */
and (
([FieldName] = "Hackney" And [Forms]![Navigation]![TEST] = True)
or
([FieldName] Not In ("Hackney") And [Forms]![Navigation]![TEST] = False)
)
Excellent stuff thanks so much that works a treat. In fact actually works fine when you look at it through Query Builder (which suits better for a variety of reasons).
For anyone else struggling with this I've taken it a bit further after this tip and linked the like/not like to a value in a combo, rather than the hard coded "Hackney" mentioned above. Also works in Query Builder, this gives my users a lot of flexibility in running their own queries properly.
Thanks again. The SQL for this is:
SELECT Clients.CLIENTSID, Clients.FirstName, Clients.LastName,
LondonBoroughs.LondonBorough, Clients.LondonBoroughID
FROM LondonBoroughs INNER JOIN Clients
ON LondonBoroughs.LONDONBOROUGHSID = Clients.LondonBoroughID
WHERE (((Clients.LondonBoroughID) Like ([Forms]![Navigation]![ReportsLondonBoroughCOMBO]))
AND (([Forms]![Navigation]![TEST])=True))
OR (((Clients.LondonBoroughID) Not Like ([Forms]![Navigation]![ReportsLondonBoroughCOMBO]))
AND (([Forms]![Navigation]![TEST])=False));

inner query of subqery returning multiple rows

I am not that experience in sql so please forgive if its not a good question to ask,but i researched around almost for 3-4 days but no able to solve.
My problem is i have a table which have multiple image names in it,so what i have to do is whoever is the follower of a particular user i have to get the imaged from this table,so one user there can be multiple followers,so i have to fetch the images posted by all the followers.
Here is the subquery code snippet i am using.
SELECT id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email=ANY(SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com')
So the inner query here returns multiple values,for each value(being_followed) i have to fetch all the images and display it,but with this query each time i get only one image.I tried IN also but didnot work out.
Table structure:-
Outfitpic_list table
id|outfit_image|datetime|Email|image_title|description
Follower_table
bring_followed|follower
Please help,I am stuck..!!
Thank you..!!
I think your problem may be the = sign between "E-mail" and "Any". Try this statement:
SELECT
id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email IN
(
SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com'
)
It's the same statement, without the = sign, and the ANY keyword replaced with IN. (I cleaned it up a little to make it more readable)

MYSQL - IN selector but getting ALL

I search my sql table using in:
videos.category in ($categoriesSelected)
I pass through the categories based on what a user selects. Sometimes when a user selects no categories I want all videos to be displayed, Ive tried changing the $categoriesSelected var to * but no luck.
Will I have to do a seperate query taking out 'in' for when I want to display all, or is there a way to change $categoriesSelected to display all? Note, I do not want to just prefil it with the names of all my categories as this constantly changes via a CMS.
If your query is built dynamically and you happen to not escape the value of $categoriesSelected
you could try videos.category IN (videos.category).
Note: this is a dummy hack and you better rewrite the whole section of code that builds the query.
You must do a check to see if something is in $categoriesSelected - if yes then do your actual query, else do the query without the IN clause
Try putting in 'SELECT category FROM categories' for your $categoriesSelected.
Change category to be the field name that stores your category name.

sfPropelPager reduce queries

i'm working in a symfony project and using sfPropelPager to show a paged list of elements.
The problem is that with a great amount of data to list (i.e. thousands of registers) it makes a query to the database for each page to show!!!! That means about 100 extra queries in my case, and that is unacceptable.
Showing some of my code: the function that returns the pager object
$pager = new sfPropelPager('MyTable',sfConfig::get('sfPropelPagerLines'));
$c = new Criteria();
$c->add('my_table_field',$value);
$c->addDescendingOrderByColumn('date');
$pager->setCriteria($c);
$pager->init();
return $pager;
So, please, if you know a way to get all the results with only one query, it would be a great solution for my problem. Otherwise i must implement that list with an ajax call for every page the user wants to see
Thank you very much for your time.
I'm not sure to get your problem but, anyway, avoid the use of Criteria. Try to make queries with the ModelCriteria API: http://www.propelorm.org/reference/model-criteria.html.
For each paginated page, a query to the database will be done, this is the standard behavior for all pagers I know. If it's related to related objects (assuming you want to display information from relations), you may want to create a query that links those objects before to paginate, that way you'll get one query per page for all your data to display.
Read this doc for instance: http://www.propelorm.org/documentation/03-basic-crud.html#query_termination_methods
At last i did'nt get a solution for the problem, i had to implement the list via AJAX call, calling to a function that returns the requested page, so at the load of the page, no query for this list is slowing the user experience.
Thank you anyway to help me :)