graphql-python / graphql-python/graphene-sqlalchemy

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

未關閉
#412 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
985
分支
223
PR 合併指標
30 天內沒有已合併 PR

描述

Hello👋,
this works as expected:
```python
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`:

```python
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

貢獻指南

開啟貢獻指南

研究方向

Run the supplied Python/SQLAlchemy/GraphQL reproduction first; it demonstrates the mismatch between GraphQL id and the model's id property. Then trace SQLAlchemyObjectType's field mapping when Python property and column names differ. Done means querying id returns 'ham' rather than 3.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
graphql, python, sqlalchemy, sqlite
領域
api, backend, databases
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。