note we are following up w/ this question at discussions: https://github.com/sqlalchemy/sqlalchemy/discussions/7303
On Mon, Nov 8, 2021, at 6:07 AM, Shefeek Najeeb wrote: > Hi all, > > It has been only a while since I've started tinkering with SQLAlchemy. I aws > working on a particular requirement of mine, where I had 4 types of users > with login functionality. But each type has different attributes. > The user types are : Client, Brand, Merchant, Customer. All these types were > supposed to have login access to the system. I created a parent model called > User and other models like Client, Brand, Merchant and Customer inherits from > the User model. I implemented it using joined table inheritance as per the > documentation. > > ============================================================ > *# User Model* > class User(UserMixin, BaseModel): > __tablename__ = "users" > id = Column(Integer, primary_key=True, autoincrement=True, unique=True) > email = Column(String, unique=True, nullable=False, index=True) > confirmed = Column(Boolean, default=False, nullable=False) > role = Column(Enum(Role)) > __mapper_args__ = { > "polymorphic_on": role, > } > ============================================================= > *# Client Model* > class Client(User): > __tablename__ = "clients" > id = Column(Integer, ForeignKey("users.id"), primary_key=True) > __mapper_args__ = { > "polymorphic_identity": "client", > 'inherit_condition': (id == User.id) > } > client_data = (...) > ..... > ================================================================ > *# Brand Model* > class Brand(User): > __tablename__ = "brands" > id = Column(Integer, ForeignKey("users.id"), primary_key=True) > __mapper_args__ = { > "polymorphic_identity": "brand", > 'inherit_condition': id == User.id > } > brand_data = (...) > ..... > > Then I created an User object, which is to serve as the admin user. It works > fine and well. > But the problem is when I'm creating other user types. For eg: when I'm > creating a Client object, a row is created in the Client table as well as the > User table. I am able to login with the client user. But the problem is when > I'm trying to access the Client object's attributes. And the object type > returned while querying the Client is of type User. > > >>> from src.models.models import User > >>> User.get_all() > [<User 63: xxxxxxx>, <User 7: xxxxxxx>, <User 67: xxxxxxxx>, <User 13: > xxxxxxxx>, <User 69: xxxxxxxxx>, <User 72: xxxxxxxx>] > >>> from src.models.models import Client > >>> > >>> Client.get_all() > [<User 7: xxxxxx>, <User 67: xxxxxxx>, <User 69: xxxxxx>] > >>> > > But if I'm making the query on Client object before loading the User, it > returns fine. > > >>> from src.models.models import Client > >>> Client.get_all() > [<Client xxxxx>, <Client xxxxxxx>, <Client xxxxxx>] > >>> > > I feel like I'm missing out on something? > > > -- > 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 view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/212bff82-4d99-4d77-a7a2-0968dcae15a1n%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/212bff82-4d99-4d77-a7a2-0968dcae15a1n%40googlegroups.com?utm_medium=email&utm_source=footer>. -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/7935a22c-4e15-4d79-8d71-662c98b8b413%40www.fastmail.com.