简体   繁体   中英

Graphene and SQLAlchemy: derivate from a base object type

I am building an app using Flask, Graphene, SQLAlchemy and Graphene-SQLAlchemy in which I implemented the following models:

class User(db.Model):
    __tablename__ = "user"
    id = Column(Integer, primary_key=True)
    name = Column(Text, nullable=False)
    email = Column(Text, nullable=False)

class BusinessUser(db.Model):
    __tablename__ = "bis_user"
    id = Column(Integer, ForeignKey("user.id"), primary_key=True)
    vatNumber = Column(Text, nullable=False)

class LVUser(db.Model):
    __tablename__ = "lv_user"
    id = Column(Integer, ForeignKey("user.id"), primary_key=True)
    lv_num = Column(Integer, nullable=False)

My goal would be to have three GraphQL types defined as such:

type User {
    id : ID
    name : String
    email : String
}

type BusinessUser {
    id : ID
    name : String
    email : String
    vatNumber : String
}

type LVUser {
    id : ID
    name : String
    email : String
    lvNum : Integer
}

But I'm struggling to find an efficient and elegant manner of doing so. Is there any way to have, let's say a "base" object type User and create BusinessUser and LVUser types on the top of my base type whilst providing the extra fields?

Please read Composing Mapped Hierarchies with Mixins which seems to describe exactly the scenario and solution to what you are looking for.

However, from the fact that you have foreign keys to user.id from other two tables hints me an inheritance which can be implemented using Mapping Class Inheritance Hierarchies , whereas the strategy will depend on how your actual tables on the database level are designed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM