[Sqlalchemy-tickets] Issue #3148: Column property labels with classes using with_polymorphic (zzzee
Brought to you by:
zzzeek
|
From: Konsta V. <iss...@bi...> - 2014-07-30 20:09:05
|
New issue 3148: Column property labels with classes using with_polymorphic https://bitbucket.org/zzzeek/sqlalchemy/issue/3148/column-property-labels-with-classes-using Konsta Vesterinen: It seems labeling column_properties with classes using with_polymorphic='*' doesn't work. The following test case outlines the problem: ```python import sqlalchemy as sa from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = sa.create_engine('sqlite:///:memory:') Base = declarative_base() Session = sessionmaker(bind=engine) session = Session() class TextItem(Base): __tablename__ = 'text_item' id = sa.Column(sa.Integer, primary_key=True) type = sa.Column(sa.Unicode(255)) __mapper_args__ = { 'polymorphic_on': type, 'with_polymorphic': '*' } class Article(TextItem): __tablename__ = 'article' id = sa.Column( sa.Integer, sa.ForeignKey(TextItem.id), primary_key=True ) __mapper_args__ = { 'polymorphic_identity': u'article' } class Tag(Base): __tablename__ = 'tag' id = sa.Column(sa.Integer, primary_key=True) text_item_id = sa.Column(sa.Integer, sa.ForeignKey(TextItem.id)) TextItem.tag_count = sa.orm.column_property( sa.select( [ sa.func.count('1') ], ) .select_from(Tag.__table__) .where(Tag.__table__.c.text_item_id == Tag.__table__.c.id) .correlate(TextItem.__table__) .label('tag_count') ) print session.query(TextItem) ``` This gives me (notice the anon_1 alias for the labeled column property): ``` SELECT text_item.id AS text_item_id, text_item.type AS text_item_type, (SELECT count(:param_1) AS count_1 FROM tag WHERE tag.text_item_id = tag.id) AS anon_1, article.id AS article_id FROM text_item LEFT OUTER JOIN article ON text_item.id = article.id ``` The expected query would be ``` SELECT text_item.id AS text_item_id, text_item.type AS text_item_type, (SELECT count(:param_1) AS count_1 FROM tag WHERE tag.text_item_id = tag.id) AS tag_count, article.id AS article_id FROM text_item LEFT OUTER JOIN article ON text_item.id = article.id ``` |