Mike,

Thanks for your careful analysis and thoughtful comments. I appreciate the 
time you spent to think about this. I agree that this does represent 
simpler syntax for a narrow class of common operations. Per your advice, 
I'll work this up into a package and post it on pypi. Thanks for the 
feedback!

Bryan

On Thursday, June 15, 2017 at 3:11:47 PM UTC-5, Bryan Jones wrote:
>
> All,
>
> While working on my SQLAlchemy-based application, I noticed an opportunity 
> to provide a more concise, Pythonic query syntax. For example, 
> User['jack'].addresses produces a Query for the Address of a User named 
> jack. I had two questions
>
>    1. Has someone already done this? If so, would you provide a link?
>    2. If not, would this be reasonable for inclusion in SQLAlchemy, 
>    either as an ORM example, or as a part of the core code base? If so, I can 
>    submit a pull request.
>
> A quick comparison of this statement to the traditional approach:
>
> User                        ['jack']                   .addresses
> Query([]).select_from(User).filter(User.name == 'jack').join(Address).
> add_entity(Address)
>
> A few more (complete) examples of this approach:
> # Ask for the full User object for jack.
> User['jack'].to_query(session)
> # Ask only for Jack's full name.
> User['jack'].fullname.to_query(session)
> # Get all of Jack's addresses.
> User['jack'].addresses.to_query(session)
> # Get just the email-address of all of Jack's addresses.
> User['jack'].addresses.email_address.to_query(session)
> # Get just the email-address j...@yahoo.com of Jack's addresses.
> User['jack'].addresses['j...@yahoo.com'].to_query(session)
> # Ask for the full Address object for j...@yahoo.com.
> Address['j...@yahoo.com'].to_query(session)
> # Ask for the User associated with this address.
> Address['j...@yahoo.com'].user.to_query(session)
> # Use a filter criterion to select a User with a full name of Jack Bean.
> User[User.fullname == 'Jack Bean'].to_query(session)
> # Use two filter criteria to find the user named jack with a full name of 
> Jack Bean.
> User['jack'][User.fullname == 'Jack Bean'].to_query(session)
> # Look for the user with id 1.
> User[1].to_query(session)
>
> Tested on Python 3.6.1, Windows 10, SQLAlchemy 1.1.10. I've attached the 
> code, and a HTML document of the code with helpful hyperlinks.
>
> Bryan
> -- 
> Bryan A. Jones, Ph.D.
> Associate Professor
> Department of Electrical and Computer Engineering
> 231 Simrall / PO Box 9571
> Mississippi State University
> Mississippi State, MS 39762
> http://www.ece.msstate.edu/~bjones
> bjones AT ece DOT msstate DOT edu
> voice 662-325-3149
> fax 662-325-2298
>
> Our Master, Jesus Christ, is on his way. He'll show up right on
> time, his arrival guaranteed by the Blessed and Undisputed Ruler,
> High King, High God.
> - 1 Tim. 6:14b-15 (The Message)
>

-- 
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