Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
usalu opened this issue Aug 17, 2024 · 0 comments

Comments

@usalu
Copy link

usalu commented Aug 17, 2024

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

@usalu usalu changed the title Query picking wrong property when property name is id with different column name and second property vice versa. Query picking shadowed property when property name is id with different column name and second property vice versa. Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant