On Mon, Dec 10, 2018 at 6:17 AM Mehrdad Pedramfar < mehrdad1373pedram...@gmail.com> wrote:
> Hi everybody, > I have three classes in my model, which one class inherited by the other > two: > > > class Item(Base): > __tablename__ = 'item' > > id = Column(Integer, primary_key=True) > title = Column(Unicode(300)) > type = Column(Unicode(50)) > > __mapper_args__ = { > 'polymorphic_on': type > } > > > class Note(Item): > __tablename__ = 'note' > > id = Column(Integer, ForeignKey('item.id'), primary_key=True) > extra = Column(Text) > > __mapper_args__ = { > 'polymorphic_identity': 'note' > } > > > class Task(Item): > __tablename__ = 'task' > > id = Column(Integer, ForeignKey('item.id'), primary_key=True) > another_extra = Column(Text) > > __mapper_args__ = { > 'polymorphic_identity': 'task' > } > So, when I execute `session.query(Item).all()` I get a list that includes > both `Note` and `Task` objects, but I don't want that, I want my objects to > be the instance of `Item` class and just have `id`, `title`, `type`, not > those extra fields. how should I write the query? > > to clarify more, currently, I get: > > > [ > <models.Note object at 0x7f25ac3ffe80>, > <models.Task object at 0x7f25ac3ffe80>, > <models.Task object at 0x7f25ac3ffe80>, > ... > ] > > But I want to get: > > [ > <models.Item object at 0x000000000000>, > <models.Item object at 0x000000000000>, > <models.Item object at 0x000000000000>, > ... > ] > TBH that's not a thing you should *want* to do. I can spend effort thinking of a few awkward ways to do that, the most direct being just get them back and change ``__class__``, but your phrase "not those extra fields" suggests it's not the type of object, but the memory/performance savings of not fetching those extra columns? What doesnt work in you program if you have a Note object and not a Task? > > I have asked the same question in StackOverflow, but still nothing. here > is the : > Get sqlalchemy base class object instead of children > <https://stackoverflow.com/questions/53458005/get-sqlalchemy-base-class-object-instead-of-children> > > Get sqlalchemy base class object instead of children > > I have three classes in my model, which one class inherited by the other > two: class Item(Base): __tablename... > > <https://stackoverflow.com/questions/53458005/get-sqlalchemy-base-class-object-instead-of-children> > > > -- > 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. > -- 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.