Skip to content

Support for Custom Column Types? #53

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
mekhami opened this issue Jun 12, 2017 · 6 comments
Closed

Support for Custom Column Types? #53

mekhami opened this issue Jun 12, 2017 · 6 comments

Comments

@mekhami
Copy link

mekhami commented Jun 12, 2017

I don't see anything in the documentation and a brief look through the source code didn't reveal anything immediately. I have a custom GUID type (as documented in the sqlalchemy docs here: http://docs.sqlalchemy.org/en/latest/core/custom_types.html#backend-agnostic-guid-type) and when I try to use the SQLAlchemyObjectType with the model metaclass, it doesn't know how to convert type GUID. Is there a way to handle these custom column types? Do I need to just subclass SQLAlchemyObjectType and make it understand what that is?

Thanks for any help.

@mekhami
Copy link
Author

mekhami commented Jun 12, 2017

Ah, I can register them the same way the converters are registered in converter.py

Sorry for the bother!

@mekhami mekhami closed this as completed Jun 12, 2017
@rodneykeeling
Copy link

Can you explain how you did this? Did you just modify the source, or was there a better way of adding another type?

@jcouser
Copy link

jcouser commented Aug 13, 2017

I just ran into this same issue as well. We have several custom subclasses of TypeDecorator from sqlalchemy.types

Currently the solution is to modify graphene_sqlalchemy/converter.py

Importing out custom types decorating the convert_column_to_string method like so:
@convert_sqlalchemy_type.register(GUID)

I'd love to know of a better solution without modifying graphene_sqlalchemy.

@mekhami
Copy link
Author

mekhami commented Aug 13, 2017

You don't have to use the convert_sqlalhemy_type.register function in graphene_sqlalchemy/converter.py. You can use it anywhere! So wherever you declare your schema, you can do this:

from my_custom_types import GUID

@convert_sqlalchemy_type.register(GUID)
def convert_column_to_string(type, column, registry=None):
    return String(description=get_column_doc(column),
                  required=not(is_column_nullable(column)))

Whenever your schema is imported and run, the conversion will also be registered.

@jcouser
Copy link

jcouser commented Aug 13, 2017

Awesome thanks for the tip. This allowed me to move the decorators and leave the module unmodified. Thanks!

@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 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants