last day (15 days later) » 

3:35 PM
1
A: How can I query data from multiple tables and sort by time?

WolphOne option: SELECT created_at FROM ( SELECT created_at FROM Post UNION ALL SELECT created_at FROM Image ) ORDER BY created_at Note that this could also be done without a subquery, but I find it clearer this way and it makes it more convenient to add filters. Technically th...

 
That only selects the created_at times, I want to be able to fetch all the columns to display it to the user. Getting ERROR: each UNION query must have the same number of columns when I change it to SELECT *
 
@kishinmanglani: that's true, you need to have the same columns in both. Note that you can "fake" columns like this: SELECT ''::text AS some_column
 
That's not a clean solution and would probably be difficult to do with SQLAlchemy. Is there no other alternative? I wish I could subclass both of these from a FeedItem parent class and query that
 
Besides changing your table structure to have common ancestor not really that much else you can do. SQLAlchemy supports this fairly easy though: docs.sqlalchemy.org/en/latest/orm/… Note that you should use union_all for performance reasons if you can :)
 
So I can have a common ancestor/parent class that would make this work? I can have a FeedItem parent class and have each of these inherit from that class? Then what would I do?
 
3:35 PM
What you are looking for in that case is polymorphic inheritance which is a rather complex matter to do efficiently, but supported by sqlalchemy: docs.sqlalchemy.org/en/latest/orm/…
Or simpler, concrete table inheritance: docs.sqlalchemy.org/en/latest/orm/…
 
It's unclear to me–will that solve my problem? If I have them inherit from a FeedItem class with an id and created_at and then each table has very different columns, will I be able to query for them sorted by created_at with all the fields?
 
@kishinmanglani: I've added an example of how you can make it work
 
seems to be working really well so far. What would the performance on something like this be? Also, would it be necessary for me to add an item_type to the FeedItem column if I wanted to do some fancy filtering? Seemed like a good idea at first, but I figured if I wanted to do filtering I could query the individual table instead.
 
Well, the with_polymorphic generates a query with a union all in it so the basics should be pretty fast. In the end it will depend on the filters you will be doing but you should be fairly safe. As for an extra column, could help for performance but probably not needed, sqlalchemy will add a virtual type column for you.
 
Thank you so much for all your help. It has been invaluable. I tried search for "virtual type column" and couldn't find a great explanation. What is that?
 
 
3 hours later…
6:25 PM
Internally the with_polymorphic calls the polymorphic_union method, which should add a type column to the results as documented here: docs.sqlalchemy.org/en/latest/orm/…
If it doesn't, a manual mapping is easily made as well
pjoin = polymorphic_union({
    'employee': employees_table,
    'manager': managers_table,
    'engineer': engineers_table
}, 'type', 'pjoin')

employee_mapper = mapper(Employee, employees_table,
                                    with_polymorphic=('*', pjoin),
                                    polymorphic_on=pjoin.c.type,
                                    polymorphic_identity='employee')
manager_mapper = mapper(Manager, managers_table,
                                    inherits=employee_mapper,
 

  last day (15 days later) »