gpt4 book ai didi

python - Graphene_sqlalchemy 和flask-sqlalchemy 对于什么构成有效的SQLAlchemy 模型存在分歧?

转载 作者:行者123 更新时间:2023-12-02 03:27:03 24 4
gpt4 key购买 nike

正在使用 Flask、Graphene,遇到了问题。考虑以下。

模型项目.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'))

模型架构(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,)

最后是我计划公开 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)

如果我在启动时加载模型,一切都很好。迁移发生,应用程序运行得很好。如果我通过架构加载模型,但应用程序会失败并显示以下消息:

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

我用flask_sqlalchemy初始化了SQLAlchemy。这让我想知道创建的模型是否不被视为有效的 SQLAlchemy 模型?或者我在这里犯了一个我没有看到的基本错误。我假设是后者。

最佳答案

根据错误消息,似乎 project.models.site (在第二个代码段中使用 from project.models import site as site_model 导入)是一个 Python模块而不是 db.Model 的子类或类似的。您是否想导入 Site(大写)而不是 site

关于python - Graphene_sqlalchemy 和flask-sqlalchemy 对于什么构成有效的SQLAlchemy 模型存在分歧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com