Utility to mark SQLAlchemy ORM columns as deprecated to allow removing them in a backwards compatible manner.
Project description
sqlalchemy-deprecated-column
Safely remove SQLAlchemy ORM columns through a gradual deprecation process. Inspired by django-deprecate-fields.
Installation
pip install sqlalchemy-deprecated-column
How it works
Removing a column from a live database requires coordination between code and schema changes. Dropping the column in a single step would break any running application instances that still reference it. deprecated_column() lets you do this safely in three steps:
- Deprecate: replace
mapped_column()withdeprecated_column()and run an Alembic migration. The column stays in the database but becomes nullable if it wasn't already. - Deploy: the column is hidden from the ORM — it no longer appears in any generated SQL. Any remaining code that reads or writes the column gets a
DeprecationWarningat runtime, making stale references easy to find and remove. - Remove: once all references are gone, delete the
deprecated_column()definition from the model and run a final migration to drop the column from the database.
Usage
Replace mapped_column() with deprecated_column() for any column you want to deprecate:
from sqlalchemy_deprecated_column import deprecated_column
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String)
old_username: Mapped[str] = deprecated_column(String) # was: mapped_column(String)
While the column is deprecated the library:
- Hides it from the ORM: the column is excluded from all generated SQL queries — existing application code stays compatible even after the column is eventually dropped from the database.
- Warns on instance read:
instance.old_usernamereturnsNoneand emits aDeprecationWarningnaming the model and column, so the call site is easy to locate. - Warns on class-level reference:
User.old_username(e.g. in filter expressions) emits aDeprecationWarningand evaluates to SQLNULL. - Warns on write and discards the value:
instance.old_username = "x"emits aDeprecationWarningand silently drops the value, so no stale data is written to the database.
Alembic integration
Add the following at the top of alembic/env.py, before any model imports:
import sqlalchemy_deprecated_column
sqlalchemy_deprecated_column.configure(alembic_mode=True)
# model imports must come after configure()
from myapp import mymodel
target_metadata = mymodel.Base.metadata
In Alembic mode, deprecated_column() acts as mapped_column(nullable=True). Alembic will:
- Not generate
DROP COLUMNfor deprecated columns. - Generate
ALTER TABLE … DROP NOT NULLif the column was originally non-nullable. This is needed because once the column is deprecated the ORM stops including it inINSERTstatements — aNOT NULLcolumn without a value would cause those inserts to fail.
Requirements
- Python 3.10+
- SQLAlchemy 2.0+
License
The code in this project is licensed under MIT license. See LICENSE for more information.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_deprecated_column-0.1.1.tar.gz.
File metadata
- Download URL: sqlalchemy_deprecated_column-0.1.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27e87769246978d4a15715a780e96cc3ef4204e48f630223906d21dd3511e09e
|
|
| MD5 |
d76feacd7dffc266085917c02ca6fe85
|
|
| BLAKE2b-256 |
0313a1ee0f675d2a6de6aaa3712528a911ca1c366ece37d996b9e8eeb4b0ca51
|
File details
Details for the file sqlalchemy_deprecated_column-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_deprecated_column-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b44ad8cd240a7df137ce260b493521c040fa3a178043568f76256d99259406c
|
|
| MD5 |
49deada9d1ecf9ae8c2191ae3e5a687e
|
|
| BLAKE2b-256 |
f07b06cc60de9482c2d0c4301061522a59859e8b81dcad251c678bac67fb1a65
|