简体   繁体   English

石墨烯和 SQLAlchemy:衍生自基础 object 类型

[英]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:我正在使用 Flask、Graphene、SQLAlchemy 和 Graphene-SQLAlchemy 构建一个应用程序,其中我实现了以下模型:

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:我的目标是定义三个 GraphQL 类型,如下所示:

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?有什么办法可以让我们说一个“基本” object 类型User并在我的基本类型的顶部创建BusinessUserLVUser类型,同时提供额外的字段?

Please read Composing Mapped Hierarchies with Mixins which seems to describe exactly the scenario and solution to what you are looking for.请阅读Composing Mapped Hierarchies with Mixins ,它似乎准确地描述了您正在寻找的场景和解决方案。

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.但是,由于您有其他两个表中user.id的外键,这提示我一个 inheritance 可以使用Mapping Class Inheritance Hierarchies来实现,而实际策略将取决于数据库级别的表的设计方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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