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

SqlAlchemy model with UUID column does not work properly #166

Open
vsipchenko opened this issue Apr 12, 2024 · 0 comments
Open

SqlAlchemy model with UUID column does not work properly #166

vsipchenko opened this issue Apr 12, 2024 · 0 comments

Comments

@vsipchenko
Copy link
Contributor

vsipchenko commented Apr 12, 2024

I have the next model

class Base(DeclarativeBase):
    __abstract__ = True
    id = Column(UUID(as_uuid=True), primary_key=True, index=True, unique=True, server_default=text("gen_random_uuid()"))

class User(Base):
    __tablename__ = "users"
    __pydantic_model__ = UserSchema
   
class UserAdmin(ModelAdmin):
    model = User

When i register this UserAdmin class i am able to view list of users in admin screen, but I can not update them, I get an exception:

hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'

For me it seems that function get_python_type_parse which is used in SqlalchemyCrud class does not handle UUID type, and in method _fetch_item_scalars it tries to pass UUID instance into UUID class

can you resolve this issue by adding UUID handler to get_python_type_parse ?

def get_python_type_parse(field: Union[InstrumentedAttribute, Column, Label]) -> Callable:
    try:
        python_type = field.expression.type.python_type
        if issubclass(python_type, UUID):
            return str
        if issubclass(python_type, datetime.date):
            if issubclass(python_type, datetime.datetime):
                return parse_datetime
            return parse_date
        return python_type
    except NotImplementedError:
        return str

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