Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Query picking shadowed property when property name is `id` with different column name and second property vice versa.

未关闭
#412 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
graphql, python, sqlalchemy, sqlite
领域
api, backend, databases

调研方向

先运行提供的 Python/SQLAlchemy/GraphQL 复现代码;它展示了 GraphQL id 与模型的 id 属性之间的不匹配。然后跟踪 Python 属性名与列名不同时 SQLAlchemyObjectType 的字段映射。完成的标准是查询 id 返回 'ham' 而不是 3。

由索引模型根据 Issue 内容生成。

描述

Hello👋,
this works as expected:

from sqlalchemy import create_engine
from sqlalchemy.orm import Session, mapped_column, Mapped, DeclarativeBase
from graphene import Field, ObjectType, Schema
from graphene_sqlalchemy import SQLAlchemyObjectType


class Base(DeclarativeBase):
    pass


class Foo(Base):
    __tablename__ = "foo"
    bar: Mapped[int] = mapped_column(
        "spam",
        primary_key=True,
    )
    spam: Mapped[str] = mapped_column("baz")


foo = Foo(bar=3, spam="ham")
engine = create_engine("sqlite:///test.sqlite3")
Base.metadata.create_all(engine)
with Session(engine) as session:
    session.add(foo)
    session.commit()


class FooNode(SQLAlchemyObjectType):
    class Meta:
        model = Foo


class Query(ObjectType):
    foo = Field(FooNode)

    def resolve_foo(self, info):
        with Session(engine) as session:
            return session.query(Foo).first()


schema = Schema(query=Query)

result = schema.execute(
    """
    {
        foo {
            spam
        }
    }
    """
)
print(result.data["foo"]["spam"])  # prints 'ham'

but as soon as you change spam to id it prints the value of the property bar:

from sqlalchemy import create_engine
from sqlalchemy.orm import Session, mapped_column, Mapped, DeclarativeBase
from graphene import Field, ObjectType, Schema
from graphene_sqlalchemy import SQLAlchemyObjectType


class Base(DeclarativeBase):
    pass


class Foo(Base):
    __tablename__ = "foo"
    bar: Mapped[int] = mapped_column(
        "id",
        primary_key=True,
    )
    id: Mapped[str] = mapped_column("baz")


foo = Foo(bar=3, id="ham")
engine = create_engine("sqlite:///test.sqlite3")
Base.metadata.create_all(engine)
with Session(engine) as session:
    session.add(foo)
    session.commit()


class FooNode(SQLAlchemyObjectType):
    class Meta:
        model = Foo


class Query(ObjectType):
    foo = Field(FooNode)

    def resolve_foo(self, info):
        with Session(engine) as session:
            return session.query(Foo).first()


schema = Schema(query=Query)

result = schema.execute(
    """
    {
        foo {
            id
        }
    }
    """
)
print(result.data["foo"]["id"])  # prints 3 but should print 'ham'

I believe that is a bug 🐛

Best, Ueli

主要语言
Python
星标
985
派生
224
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

graphql-python/graphene-sqlalchemy 的其他 Issue

查看 graphql-python/graphene-sqlalchemy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。