简体   繁体   English

Graphene_sqlalchemy和flask-sqlalchemy在什么构成有效SQLAlchemy模型上存在分歧。

[英]Graphene_sqlalchemy and flask-sqlalchemy disagree on what constitutes a Valid SQLAlchemy Model?

Playing with Flask, Graphene and am running into a problem. 玩Flask,Graphene并遇到问题。 Consider the following. 考虑以下。

The Model project.model.site: Model project.model.site:

from project import db
from project.models import user
from datetime import datetime

class Site(db.Model):
    __tablename__ = 'sites'
    id = db.Column(db.Integer(), primary_key=True)
    owner_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    name = db.Column(db.String(50))
    desc = db.Column(db.Text())
    location_lon = db.Column(db.String(50))
    location_lat = db.Column(db.String(50))
    creation_date = db.Column(db.DateTime(), default=datetime.utcnow())
    users = db.relationship(
        user,
        backref=db.backref('users',
                        uselist=True,
                        cascade='delete,all'))

The model schema (project.schemas.site_schema) 模型架构(project.schemas.site_schema)

from graphene_sqlalchemy import SQLAlchemyObjectType
from project.models import site as site_model
import graphene

class SiteAttributes:
    owner_id = graphene.ID(description="Site owners user.id")
    name = graphene.String(description="Site Name")
    desc = graphene.String(description="Site description")
    location_lon = graphene.String(description="Site Longitude")
    location_lat = graphene.String(description="Site Latitude")
    creation_date = graphene.DateTime(description="Site Creation Date")

class Site(SQLAlchemyObjectType, SiteAttributes):
    """Site node."""
    class Meta:
        model = site_model
        interfaces = (graphene.relay.Node,)

and finally the main schema through which I plan to expose the GraphQL api (project.schemas.schema)) 最后是我计划公开GraphQL api的主要模式(project.schemas.schema)

from graphene_sqlalchemy import SQLAlchemyConnectionField
import graphene
from project.schemas import site_schema, trade_schema, user_schema

class Query(graphene.ObjectType):
    """Query objects for GraphQL API."""

    node = graphene.relay.Node.Field()
    user = graphene.relay.Node.Field(user_schema.User)
    userList = SQLAlchemyConnectionField(user_schema.User)
    site = graphene.relay.Node.Field(site_schema.Site)
    siteList = SQLAlchemyConnectionField(site_schema.Site)
    trade = graphene.relay.Node.Field(trade_schema.Trade)
    tradeList = SQLAlchemyConnectionField(trade_schema.Trade)


schema = graphene.Schema(query=Query)

If I load the model as such when starting up all is well. 如果我在启动时这样加载模型,一切都很好。 Migrations happen, the application runs perfectly fine. 发生迁移后,应用程序运行正常。 If I load the model through the schema though the application fails with the following message: 如果我通过架构加载模型,但是应用程序失败并显示以下消息:

AssertionError: You need to pass a valid SQLAlchemy Model in Site.Meta, received "<module 'project.models.site' from '/vagrant/src/project/models/site.py'>".

I initialized SQLAlchemy with flask_sqlalchemy. 我用flask_sqlalchemy初始化了SQLAlchemy。 Which makes me wonder is the model that is created not considered a valid SQLAlchemy Model ? 这让我怀疑是不是将创建的模型视为有效的SQLAlchemy模型? Or am I doing a basic error here that I am just not seeing. 还是我在这里做一个基本的错误,而我只是没有看到。 I am assuming it's the latter. 我认为是后者。

Based on the error message, it seems that project.models.site (imported in the second snippet with from project.models import site as site_model ) is a Python module rather than a subclass of db.Model or similar. 根据错误消息,似乎project.models.site (在第二个片段中from project.models import site as site_model )是Python模块,而不是db.Model或类似的子类。 Did you perhaps mean to import Site (uppercase) instead of site ? 您也许是要导入Site (大写)而不是site

So fixing packages to classes finally go me in the right direction. 因此,将软件包固定到类上最终使我朝着正确的方向发展。 It turns out that the issue was deeper then that. 事实证明,问题要深得多。 And the only way to get to it was by reading the hidden exceptions. 唯一的方法就是读取隐藏的异常。

First I ensured that actual models where loaded rather than the modules. 首先,我确保加载的是实际模型,而不是模块。 Thank you So much for that one @jwodder 非常感谢你@jwodder

In the end this https://github.com/graphql-python/graphene-sqlalchemy/issues/121 was ended up pointing me in the right direction. 最后,这个https://github.com/graphql-python/graphene-sqlalchemy/issues/121最终指向正确的方向。 By checking the actual exception messages I found my way to a solution 通过检查实际的异常消息,我找到了解决方案的方法

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

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