Skip to content

Document using SQLAlchemy column types; add pydantic.EmailStr support #762

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

rmasters
Copy link

@rmasters rmasters commented Dec 27, 2023

When I start a new project, I typically start with a User model and always bump into this error:

ValueError: <class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type

Today I decided to dig into this, and found that EmailStr doesn't subclass str, which explains why it needs a Field(sa_type=String()). I noticed the AutoString types for ipaddress & pathlib, and figured EmailStr might be popular enough to be worth adding (example discussion).

As part of this, in order to test that change I've started documenting how SA column types work in SQLModel. I also noticed the mention about varchar length and documented that too.

Let me know if you'd prefer this as two separate PRs and I can cherry-pick it out - they're only together as I wanted to use the docs-tutorial tests (which are awesome!)


  • Add pydantic.EmailStr support to get_sqlalchemy_type
  • Add documentation for column types, including varchar/text string lengths
  • Finish testing "exotic sqlalchemy type" (geoalchemy2) tests
    • I originally had some test cases for GIS using geoalchemy2's SQLAlchemy column types. Testing this with sqlite requires libspatialite, which probably needs some CI work/containerization to add this to the environment. I'll revisit this as a separate advanced tutorial in another PR if there's interest.
  • Finish testing ipaddress, pathlib & EmailStr AutoString type alises
  • Rebase commits to a clean history

Copy link

📝 Docs preview for commit 8bd9068 at: https://23112ba0.sqlmodel.pages.dev

@rmasters rmasters force-pushed the convert-emailstr-to-autostring branch from 8bd9068 to 165e9c5 Compare December 27, 2023 13:26
Copy link

📝 Docs preview for commit 0e8c5bc at: https://cb2b259d.sqlmodel.pages.dev

Comment on lines +11 to +12
source_ip_address: IPvAnyAddress
upload_location: Path
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised to find sqlmodel/pydantic don't convert these values to string for persistence or back into the IP/Path types on retrieval. I think the solution is to implement a Pydantic type, but perhaps I'm doing something wrong here?

Copy link

@bschoenmaeckers bschoenmaeckers Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bug in the pydantic v2 implementation because it used to convert it back to native python types. I've seen it happen with datetimes as well.
Edit: never mind, this has been a problem for quick a while.

@rmasters rmasters force-pushed the convert-emailstr-to-autostring branch from 0e8c5bc to 9964b07 Compare December 27, 2023 13:44
Copy link

📝 Docs preview for commit 7aead7f at: https://f76c7357.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit bae753d at: https://c988b4c3.sqlmodel.pages.dev

@rmasters rmasters force-pushed the convert-emailstr-to-autostring branch from e841517 to 8cd7c17 Compare December 31, 2023 15:31
Copy link

📝 Docs preview for commit 8cd7c17 at: https://2b2d516a.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit 8c45261 at: https://8390b32c.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit 9a0c172 at: https://fe6eeaf3.sqlmodel.pages.dev

@mockingjet
Copy link

Any update on this MR?

@alejsdev alejsdev added the docs Improvements or additions to documentation label Jul 12, 2024
@nameer
Copy link

nameer commented Feb 8, 2025

Bump! This is much needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants