Skip to content

"Can't generate DDL for NullType()" when using pydantic.SecretStr #1514

Answered by redb0
maxmouchet asked this question in Questions
Discussion options

You must be logged in to vote

I recently came across a similar task and came across this question.

I understand that it has been a long time, but it may be useful to someone.

You can use TypeDecorator from sqlalchemy (https://docs.sqlalchemy.org/en/20/core/custom_types.html#sqlalchemy.types.TypeDecorator)

It will look something like this

import sqlalchemy as sa

class SecretStrType(sa.types.TypeDecorator):
    impl = sa.types.TEXT

    def process_bind_param(self, value: SecretStr, dialect):
        return value.get_secret_value()

    def process_result_value(self, value: str, dialect):
        return SecretStr(value)

and full example

from typing import Optional

import sqlalchemy as sa
from pydantic import SecretStr
f…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

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

This discussion was converted from issue #112 on August 13, 2025 10:47.