Fix column alias handling in aggregated
This commit is contained in:
@@ -394,10 +394,11 @@ class AggregatedAttribute(declared_attr):
|
|||||||
self.relationship = relationship
|
self.relationship = relationship
|
||||||
|
|
||||||
def __get__(desc, self, cls):
|
def __get__(desc, self, cls):
|
||||||
|
value = (desc.fget, desc.relationship, desc.column)
|
||||||
if cls not in aggregated_attrs:
|
if cls not in aggregated_attrs:
|
||||||
aggregated_attrs[cls] = [(desc.fget, desc.relationship)]
|
aggregated_attrs[cls] = [value]
|
||||||
else:
|
else:
|
||||||
aggregated_attrs[cls].append((desc.fget, desc.relationship))
|
aggregated_attrs[cls].append(value)
|
||||||
return desc.column
|
return desc.column
|
||||||
|
|
||||||
|
|
||||||
@@ -577,7 +578,7 @@ class AggregationManager(object):
|
|||||||
|
|
||||||
def update_generator_registry(self):
|
def update_generator_registry(self):
|
||||||
for class_, attrs in six.iteritems(aggregated_attrs):
|
for class_, attrs in six.iteritems(aggregated_attrs):
|
||||||
for expr, relationship in attrs:
|
for expr, relationship, column in attrs:
|
||||||
relationships = []
|
relationships = []
|
||||||
rel_class = class_
|
rel_class = class_
|
||||||
|
|
||||||
@@ -589,7 +590,7 @@ class AggregationManager(object):
|
|||||||
self.generator_registry[rel_class].append(
|
self.generator_registry[rel_class].append(
|
||||||
AggregatedValue(
|
AggregatedValue(
|
||||||
class_=class_,
|
class_=class_,
|
||||||
attr=expr.__name__,
|
attr=column,
|
||||||
relationships=list(reversed(relationships)),
|
relationships=list(reversed(relationships)),
|
||||||
expr=expr(class_)
|
expr=expr(class_)
|
||||||
)
|
)
|
||||||
|
58
tests/aggregate/test_with_column_alias.py
Normal file
58
tests/aggregate/test_with_column_alias.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy_utils.aggregates import aggregated
|
||||||
|
from tests import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestAggregatedWithColumnAlias(TestCase):
|
||||||
|
def create_models(self):
|
||||||
|
class Thread(self.Base):
|
||||||
|
__tablename__ = 'thread'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
|
||||||
|
@aggregated(
|
||||||
|
'comments',
|
||||||
|
sa.Column('_comment_count', sa.Integer, default=0)
|
||||||
|
)
|
||||||
|
def comment_count(self):
|
||||||
|
return sa.func.count('1')
|
||||||
|
|
||||||
|
comments = sa.orm.relationship('Comment', backref='thread')
|
||||||
|
|
||||||
|
class Comment(self.Base):
|
||||||
|
__tablename__ = 'comment'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
thread_id = sa.Column(sa.Integer, sa.ForeignKey('thread.id'))
|
||||||
|
|
||||||
|
self.Thread = Thread
|
||||||
|
self.Comment = Comment
|
||||||
|
|
||||||
|
def test_assigns_aggregates_on_insert(self):
|
||||||
|
thread = self.Thread()
|
||||||
|
self.session.add(thread)
|
||||||
|
comment = self.Comment(thread=thread)
|
||||||
|
self.session.add(comment)
|
||||||
|
self.session.commit()
|
||||||
|
self.session.refresh(thread)
|
||||||
|
assert thread.comment_count == 1
|
||||||
|
|
||||||
|
def test_assigns_aggregates_on_separate_insert(self):
|
||||||
|
thread = self.Thread()
|
||||||
|
self.session.add(thread)
|
||||||
|
self.session.commit()
|
||||||
|
comment = self.Comment(thread=thread)
|
||||||
|
self.session.add(comment)
|
||||||
|
self.session.commit()
|
||||||
|
self.session.refresh(thread)
|
||||||
|
assert thread.comment_count == 1
|
||||||
|
|
||||||
|
def test_assigns_aggregates_on_delete(self):
|
||||||
|
thread = self.Thread()
|
||||||
|
self.session.add(thread)
|
||||||
|
self.session.commit()
|
||||||
|
comment = self.Comment(thread=thread)
|
||||||
|
self.session.add(comment)
|
||||||
|
self.session.commit()
|
||||||
|
self.session.delete(comment)
|
||||||
|
self.session.commit()
|
||||||
|
self.session.refresh(thread)
|
||||||
|
assert thread.comment_count == 0
|
Reference in New Issue
Block a user