Skip to content

Conversation

ravishan16
Copy link

Closes #447

This pull request adds documentation for using EncryptedType with SQLModel. It includes:

  • A new tutorial with a detailed walkthrough.
  • A new page in the "Advanced User Guide" for quick reference.

@github-actions github-actions bot added the docs Improvements or additions to documentation label Sep 25, 2025
Copy link
Contributor

@williambdean
Copy link

I am seeing in the sqlalchemy-utils docs that EncryptionType is deprecating in favor StringEncryptedType. Maybe good to swap out. https://sqlalchemy-utils.readthedocs.io/en/latest/data_types.html#encryptedtype

@YuriiMotov YuriiMotov changed the title feat: Add tutorial and advanced page for EncryptedType 📝 Add tutorial and advanced page for EncryptedType Oct 6, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

@ravishan16, thank you for your interest and efforts!

The example is interesting, but I think this should be a section of docs about sa_column property (the code example boils down to just using sa_column to specify SQLAlchemy type for the field).

I also don't think we should add 2 sections about this. I think it should be in Advanced section only.

What do you think?

Copy link
Contributor

github-actions bot commented Oct 9, 2025

📝 Docs preview

Last commit 692ae8f at: https://f5aeaf54.sqlmodel.pages.dev

Modified Pages

…ueness tutorials; remove deprecated encrypted-type page; update nav and run notes
@ravishan16 ravishan16 force-pushed the docs-encrypted-type branch from f4ae1b4 to 00b594f Compare October 9, 2025 03:49
@ravishan16
Copy link
Author

@ravishan16, thank you for your interest and efforts!

The example is interesting, but I think this should be a section of docs about sa_column property (the code example boils down to just using sa_column to specify SQLAlchemy type for the field).

I also don't think we should add 2 sections about this. I think it should be in Advanced section only.

What do you think?

Thanks @YuriiMotov for the thoughtful feedback, completely agree. I’ve reworked this into a single Advanced page focused on sa_column. The encryption piece is now just one example of passing a fully configured SQLAlchemy Column, rather than a standalone “encrypted type” doc.

Summary of changes:
• Consolidated content into a single Advanced doc: sa-column.md
• Replaced the deprecated EncryptedType with sqlalchemy_utils.StringEncryptedType (with a short note explaining why) as suggested by @williambdean
• Added a second small example to show versatility: enforcing uniqueness via sa_column and a composite UniqueConstraint
• Ensured examples are runnable and consistent
• Removed the old encrypted-type page and updated navigation accordingly

If you’d prefer to keep just one example (e.g., only the encrypted field) or adjust the tone/titles, I’m happy to trim or tweak as needed or you can change it as needed.

@github-actions github-actions bot removed the waiting label Oct 9, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

@ravishan16, thank you!

It's already very helpful! But I think we should try to make the style more consistent with other sections. Please, take a look at my comments below.

As for enforcing uniqueness use case - I don't see any advantage of using Column here. I would remove this part to not overload docs with. We can later add more use-cases.

Also, I think we should create a sub-group inside Advanced section like Advanced Column Configuration (let's think about the name, this is just a suggestion) where we will place all things related to customization with sa_column, sa_column_args, sa_type, sa_column_kwargs.


Note: `StringEncryptedType` provides explicit string type handling and better compatibility with SQLAlchemy 2.x.

{* ./docs_src/advanced/sa_column/tutorial001.py *}
Copy link
Member

@YuriiMotov YuriiMotov Oct 9, 2025

Choose a reason for hiding this comment

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

It would be nice to explain this in a bit more details, make it a bit more entertaining and step-by-step.
Something like (very raw draft):

Remember we have secret_name fields for every Hero?
What if the DB leakage happens and all data becomes public? Everyone will know secret names of our heroes.. 😱
Let's try to mitigate such security risks by introducing data encryption for sensitive data.
All we need is to use StringEncryptedType DB type for secret_name

An then add a part of code example (only related lines with most important lines highlighted) like this

{* ./docs_src/advanced/sa_column/tutorial001.py ln[1:28] hl[4:8,11:12,20:26] *}

Then describe in short what is happening in select_heroes.

Comment on lines +48 to +51
```bash
python -m venv .venv
source .venv/bin/activate
pip install sqlmodel sqlalchemy-utils cryptography
Copy link
Member

Choose a reason for hiding this comment

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

I think we should use standardized text for installation part (see how it's done for fastapi-related section) and move it to the beginning (inside ## Use case: encrypted field with a custom type)

## Use case: encrypted field with a custom type

### Install `sqlalchemy-utils` and `cryptography` packeges

The first step is to install `sqlalchemy-utils` and `cryptography` packeges.

Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with:

<div class="termy">

```console
$ pip install sqlmodel sqlalchemy-utils cryptography

---> 100%
```

</div>

ravishan16 and others added 2 commits October 9, 2025 13:34
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
@github-actions github-actions bot removed the waiting label Oct 10, 2025
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.

3 participants