I'm trying to use the function distinct with SQLAlchemy but it doesn’t seem to work. I prepared a little example where you can see my problem:
#-*- coding: utf-8 -*-
from sqlalchemy import create_engine,Column, Integer
from sqlalchemy.orm import sessionmaker,load_only
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class my_class(Base):
__tablename__ = 'my_table'
id= Column(Integer, primary_key=True)
data= Column(Integer)
Base.metadata.create_all(engine)
for i in range(10):
p=my_class()
p.id=i
p.data=55
session.add(p)
session.commit()
s=session.query(my_class).distinct(my_class.data).options(load_only(my_class.data))
print (s)
for a in s.all():
print (a.id,a.data)
Executing this I would expect an output like this:
SELECT my_table.data AS my_table_data
FROM my_table
None 55
But instead I’m getting this:
SELECT DISTINCT my_table.id AS my_table_id, my_table.data AS my_table_data
FROM my_table
0 55
1 55
2 55
3 55
4 55
5 55
6 55
7 55
8 55
9 55
What I'm doing wrong?