Skip to content

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'Color' #1497

Answered by byrman
masreplay asked this question in Questions
Discussion options

You must be logged in to vote

You could use a validator

class Foo(SQLModel, table=True):
    class Config:
        validate_assignment = True

    id: Optional[int] = Field(default=None, primary_key=True)
    color: Color = Field(sa_column=Column(AutoString()))

    @validator("color")
    def color_as_hex(cls, v):
        return v.as_hex()

if you don't mind that your color attribute will become a string after validation. Or you could write a custom ColorType:

class ColorType(types.TypeDecorator):
    impl = types.TEXT

    def process_bind_param(self, value, dialect):
        return value.as_hex()

    def process_result_value(self, value, dialect):
        return Color(value)


class Bar(SQLModel, table=True):
    …

Replies: 7 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@YuriiMotov
Comment options

Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
4 participants
Converted from issue

This discussion was converted from issue #212 on August 12, 2025 15:35.