Skip to content

No UUIDType support #336

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

Closed
calibodhi opened this issue Apr 11, 2022 · 4 comments · Fixed by #353
Closed

No UUIDType support #336

calibodhi opened this issue Apr 11, 2022 · 4 comments · Fixed by #353

Comments

@calibodhi
Copy link

Almost identical to #257
Opening a new issue since I could not find a solution. Any help is much appreciated.

from sqlalchemy_utils import UUIDType

class Item(Base):
    id = Column(binary=False, native=True), primary_key=True, default=uuid4)

from graphene_sqlalchemy import SQLAlchemyObjectType

class ItemNode(SQLAlchemyObjectType):
    class Meta:
        model = Item
Exception: Don't know how to convert the SQLAlchemy field item.id (<class 'sqlalchemy.sql.schema.Column'>)

How can I support UUIDs?

@calibodhi calibodhi changed the title No UUIDType support No UUIDType support (sqlite3) Apr 11, 2022
@erikwrede
Copy link
Member

It would be interesting to see if this works with the native Postgres.UUID instead of sqlalchemy_utils.UUID, since internal converters exist for that Type (see converters.py line 188).
On the other hand, the work-around in #257 should definitely working for IDs then... I'll try to reproduce this once I find the time.

For now, it would be helpful if you could post a traceback of the error for further analysis.

@calibodhi calibodhi changed the title No UUIDType support (sqlite3) No UUIDType support Apr 30, 2022
@calibodhi
Copy link
Author

calibodhi commented Apr 30, 2022

@erikwrede thanks! It works with the postgres UUID on a postgres db. I would love to get this working with sqlite3. I will open a separate issue for that, if needed, and change the name back on this since I referenced the other issues.

from uuid import uuid4
from sqlalchemy.dialects.postgresql import UUID

class Item(Base):
   id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)

from graphene_sqlalchemy import SQLAlchemyObjectType

class ItemNode(SQLAlchemyObjectType):
    class Meta:
        model = Item

@erikwrede
Copy link
Member

erikwrede commented May 1, 2022

Sorry for the late reply. I haven't found the time to do any testing earlier. Since you referenced #257, I expected that you already tried adding a custom type converter for UUIDType. This is necessary because SQLAlchemy does not natively support sqlalchemy_utils.UUIDType and does not know how to convert this time into a Graphene-Type for the GraphQL-Schema.
You are seeing the error Message Exception: Don't know how to convert the SQLAlchemy field item.id (<class 'sqlalchemy.sql.schema.Column'>) because of that.

Adding a custom type converter is as simple as adding these lines to your code before the model:

from sqlalchemy_utils import UUIDType
from graphene_sqlalchemy.converter import convert_sqlalchemy_type
@convert_sqlalchemy_type.register(UUIDType)
def convert_column_to_string(type, column, registry=None):
    return graphene.String

This is essentially the solution detailed in #257. I've just tested this with your code above on graphene-sqlalchemy==3.0.0b1 and verified that it works!
I could not reproduce any of the other problems mentioned in #257. Try it out and let me know if it worked!

Quick side note: To prevent this from happening in most of the cases, I want to add additional type converters to graphene-sqlalchemy 3.0. Tracking in #339.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants