Re: [sqlalchemy] Distinct within group by query

2019-09-04 Thread Varun Madiath
What database are you using? Some databases might support this natively, others would have to be emulated by the ORM. I’m not a big user of the ORM, so someone else would have to help you with that. If you’re using Postgres, then the array_agg function on the color column will give you exactly

Re: [sqlalchemy] Distinct within group by query

2019-09-04 Thread Andrew M
Thank you Varun - what you've shown in the table is exactly what I'm looking for. -- 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

Re: [sqlalchemy] Distinct within group by query

2019-09-04 Thread Varun Madiath
Just so I understand what you're asking? You want the minimum price per product (across all colors), as well as the list of colors? A result set looking something like this? | Attribute | Min Price | Colors | |---|---|--| | Attr_1| 10|

[sqlalchemy] Distinct within group by query

2019-09-04 Thread Andrew M
Hi, I would like to use distinct inside a group_by query, e.g.: session.query(Product.attribute_x, func.min(Product.price), distinct(Product.color)). group_by(Product.attribute_x) That is, for each value of attribute_x, I want to find the lowest price and all of the colors available. I don't

Re: [sqlalchemy] Re: Complex Constraints in Many to Many relationships

2019-09-04 Thread Michael P. McDonnell
So I must be missing something, but here's what I have right now: tournament_table = Table( 'tournament', Base.metadata, Column('id', UUID(as_uuid=True), primary_key=True)) team_table = Table( 'team', Base.metadata, Column('id', UUID(as_uuid=True), primary_key=True, *

[sqlalchemy] Re: Complex Constraints in Many to Many relationships

2019-09-04 Thread Derek Lambert
If I'm understanding correctly... You're on the right track. I'd use a composite primary key on |team_person|, consisting of foreign keys from |person| and |team|, and another composite key (or unique index) on the |team| to |tournament| table. This lets the database do all the work. -Derek

[sqlalchemy] Complex Constraints in Many to Many relationships

2019-09-04 Thread Michael P. McDonnell
Hey - I'm again at a loss of what to google, and as this will ultimately need to be represented in some fashion in sqlalchemy, I figured this is a great place to start: I have a |person| table and a |team| table with a many to many table in between |team_person|. Simple enough! Now - to make it