python - 如何在 eve-sqlalchemy 中查询嵌套集合中的资源?

标签 python mongodb sqlalchemy eve

我正在使用Eve-SQLAlchemy==0.5.0

我想使用 Postman 对我的用户执行嵌套查询,以便找到指定组织内的所有用户。

使用 SQL,我会编写这样的查询:

select * from app_user
left join user_organization on user_organization.user_id = app_user.id
left join organization on organization.id = user_organization.organization_id
where organization.id = 2
<小时/>

我有一个用户模型、一个组织模型和一个链接两个 user_organization 的关系模型。

from sqlalchemy import Column, DateTime, func, String, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class BaseModel(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    __abstract__ = True
    _created = Column(DateTime, default=func.now())
    _updated = Column(DateTime, default=func.now(), onupdate=func.now())
    _etag = Column(String(40))


class User(BaseModel):
    __tablename__ = 'app_user'
    organizations = relationship("Organization", secondary=UserOrganization.__tablename__)


class Organization(BaseModel):
    __tablename__ = 'organization'
    name = Column(String)


class UserOrganization(BaseModel):
    __tablename__ = 'user_organization'
    user_id = Column(Integer,
                     ForeignKey('app_user.id', ondelete='CASCADE'))
    organization_id = Column(Integer,
                             ForeignKey('organization.id', ondelete='CASCADE'))

在我的 settings.py 中,我注册了资源:

# Resource Registration
DOMAIN = DomainConfig({
    'organization': ResourceConfig(Organization),
    'user': ResourceConfig(User)
}).render()
<小时/>

我有一系列 postman 集合设置,并且使用 GET 请求我可以轻松查询任何属性... GET localhost:5000/user?where={"id":1}

我已经尝试过(以及其他许多事情):

GET user?where={"organizations": {"organization_id" :2 }}
GET user?where={"organizations": 2}

最佳答案

由于错误,目前似乎无法实现。我会尽力在下周内修复它。

https://github.com/pyeve/eve-sqlalchemy/blob/master/eve_sqlalchemy/parser.py#L73中的代码导致 GET ?where={"organizations": 2} 生成类似于 user_id = 42 AND Organization_id = 42 的 SQL 表达式。这几乎没有任何意义。

关于python - 如何在 eve-sqlalchemy 中查询嵌套集合中的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50298923/

相关文章:

Mongodb 在 Debian 上失败

python - SQLAlchemy 和 Google Cloud Sql session 池

java - 我如何知道 ArrayList 是否包含 mongoDB 中的特定项目?

postgresql - 如何为点几何创建地质炼金术表达式?

python - 与 PostgreSQL 数据库的 SQLAlchemy/psycopg2 连接是否加密

python - python selenium 驱动程序中的 "Message: unknown error: cannot focus element"

python - Kivy 将小部件添加到屏幕

python - 在 python 中启动 Twisted.internet.reactor

Python向list添加多个列表

mysql - 从 MongoDB 应用程序访问 ActiveRecord 模型