On 01/17/2017 01:05 PM, Chris Withers wrote:
Potentially related issue, given:

class TheTable(Base):
    __tablename__ = 'stuff'
    __mapper_args__ = dict(
        polymorphic_on='type',
        polymorphic_identity='base',
    )
    type = Column(String)
    col = Column(String)

class Model1(TheTable):
    __mapper_args = dict(
        polymorphic_identity='model1',
    )

class Model2(TheTable):
    __mapper_args = dict(
        polymorphic_identity='model2',
    )

session.add(Model1(col='a'))
session.add(Model2(col='b'))
session.flush()

I'd now expect:

self.session.query(Model1)

...to turn the Model1 instance, but in my tests it returns nothing.

you misspelled "__mapper_args__"



self.session.query(TheTable) returns both rows, but both as TheTable
instances, which I find a little surprising.

Chris

On 16/01/2017 17:54, mike bayer wrote:
issue

https://bitbucket.org/zzzeek/sqlalchemy/issues/3891/single-inh-criteria-should-be-added-for



is added.   Targeted at 1.2 as it will break applications unknowingly
relying upon the bug right now.

For now say func.count(Manager.employee_id), e.g. put the entity in the
columns clause.



On 01/16/2017 12:23 PM, Michael Williamson wrote:
Hello!

I have a use case where I want to select from a polymorphic table, but
without selecting any columns from that table. As a simple example,
consider selecting the count of all rows. When I write something like:

    sess.query(func.count(1)).select_from(Manager).all()

It seems to be equivalent to:

    sess.query(func.count(1)).select_from(Employee).all()

(where Manager inherits from Employee).

Is this intended, or is this a bug? If the former, what's the suggested
approach to writing such queries? To filter on the discriminator
explicitly?

For reference, I was able to reproduce the issue with a test case in
test/orm/inheritance/test_single.py:

    def test_select_from_inherited_tables(self):
        Manager, Engineer, Employee = (self.classes.Manager,
self.classes.Engineer, self.classes.Employee)

        sess = create_session()
        m1 = Manager(name='Tom', manager_data='data1')
        e1 = Engineer(name='Kurt', engineer_info='knows how to hack')
        sess.add_all([m1, e1])
        sess.flush()

        eq_(
            sess.query(func.count(1)).select_from(Manager).all(),
            [(1, )]
        )

Thanks

Michael




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