fastapi-amis-admin icon indicating copy to clipboard operation
fastapi-amis-admin copied to clipboard

SqlAlchemy model with UUID column does not work properly

Open vsipchenko opened this issue 2 months ago • 0 comments

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

vsipchenko avatar Apr 12 '24 14:04 vsipchenko