graphql-python / graphql-python/graphene-sqlalchemy

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

Đang mở
#412 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
985
Fork
223
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.