Brings transparent soft delete to SQLAlchemy ORM
Project description
This fork has been updated to work with SQLAlchemy 3.
Brings transparent soft delete to SQLAlchemy ORM. This branch has been modified to add the with_deleted() and restore() methods for interacting with soft-deleted resources.
Installation
pip install sqla-paranoid3
Usage
from paranoid.models import (
Model,
Query,
Session,
)
class User(Model):
__tablename__ = 'user'
__softdelete__ = True
id = Column(Integer, primary_key=True)
name = Column(String)
engine = create_engine('sqlite://')
session = sessionmaker(engine, class_=Session, query_cls=Query)()
session.query(User)
# retrieve a user that has not been deleted (assuming ID is 1)
user = User.query.get(1)
# soft delete the user
user.delete()
# query for a soft deleted user
user = User.query.with_deleted().get(1)
# or
user = User.query.with_deleted().filter_by(id=1).first()
# restore the user (undo soft delete)
user.restore()
# save the changes
session.add(user)
session.commit()
# hard delete the user
session.delete(user, hard=True)
session.commit()
Flask
Paranoid3 comes with a ready to use Flask extension built on top of Flask-SQLAlchemy:
from paranoid.flask import SQLAlchemy
db = SQLAlchemy(app)
# or you can use the factory style of initialisation
db = SQLAlchemy()
db.init_app(app)
Model = db.Model
class User(Model):
__softdelete__ = True
id = Column(Integer, primary_key=True)
name = Column(String)
User.query
Project details
Release history Release notifications | RSS feed
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 sqla_paranoid3-0.1.4.tar.gz.
File metadata
- Download URL: sqla_paranoid3-0.1.4.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
354bc6a3d9c4af60b091c783e986784bd870c1f64c5dcc0679a661304e72ccce
|
|
| MD5 |
7142465a6f2ff7ee1edd559142b0786f
|
|
| BLAKE2b-256 |
aba7b4e5eab424d6fc1e75466a7c6a03689c4a9cc0b87910444b039aa69fed8b
|
File details
Details for the file sqla_paranoid3-0.1.4-py3-none-any.whl.
File metadata
- Download URL: sqla_paranoid3-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14b979fdcf5d6e683df4148cca59da377a8b13e28785c069fef2e755cf93ef05
|
|
| MD5 |
eb4a67bf370d4568c1f7daa1d3b7b602
|
|
| BLAKE2b-256 |
f487d6eae9dce00dd9517e9aa2c1b3fbc606ee8016f319a6c84ca71bdcc0037b
|