Hello, i'm supporting legacy database, and using such models (code contains
examples of my problem, keep reading):
class Product: # This table is never used straightforward in queries
type = Column(db.Integer)
name = Column(db.String)
TYPE_GUITARS = 1
TYPE_DRUMS = 2
__tablename__ = 'product'
__mapper_args__ = {
'polymorphic_on': type,
}
class Guitars(Product):
__mapper_args__ = {
'polymorphic_identity': Product.TYPE_GUITARS, # Guitars entity
}
class Drums(Product):
__mapper_args__ = {
'polymorphic_identity': Product.TYPE_DRUMS, # Drums entity
}
# This query returns nothing.
query1 = session.query(Musician).filter(
Musician.guitar == Guitars.id,
Musician.drum == Drums.id,
)
# Because SQL query built is wrong:
# select * ... *from product* where ... => no rows returned
# To fix his i use aliased() function:
guitars = aliased(Guitars, name='guitars')
drums = aliased(Drums, name='drums')
query2 = session.query(Musician).filter(
Musician.guitar == guitars.id,
Musician.drum == drums.id,
)
# select * ... *from product as guitars, product as drums* where ... =>
working fine
My problem is that when i'm using two Polymorphic Models (with one database
table) i always must use aliased() function. This is demonstrated in
example higher.
As you can see, query1 produces no results, because it is selecting from
*products* table, and applies two filters to it. Instead it should use *two
product table aliases. *It can be achieved with using *aliased()* function.
So, finally, my question is:
Are there any mechanism in sqlalchemy that can automatically apply aliases
to such tables, if it sees their combination in query?
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.