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

v2 URL cannot be casted to DB #818

Closed
gaborbernat opened this issue Jul 24, 2023 · 3 comments
Closed

v2 URL cannot be casted to DB #818

gaborbernat opened this issue Jul 24, 2023 · 3 comments
Assignees

Comments

@gaborbernat
Copy link

gaborbernat commented Jul 24, 2023

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'pydantic_core._pydantic_core.Url'

This used to work in v1, translating to str automatically.

Selected Assignee: @adriangb

@adriangb
Copy link
Member

I’m guessing it’s because it’s not a subclass of str. I don’t use SQLAlchemy but isn’t there a way to register conversions? Calling str() on it should suffice.

@adriangb
Copy link
Member

I think you'll have to do something like:

from psycopg2.extensions import register_adapter, AsIs
from pydantic_core import Url

def adapt_pydantic_url(url):
    return AsIs(str(url))

register_adapter(Url, adapt_pydantic_url)

@davidhewitt
Copy link
Contributor

Related notes from the migration guide:

https://docs.pydantic.dev/latest/migration/#url-and-dsn-types-in-pydanticnetworks-no-longer-inherit-from-str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants