Skip to main content

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.

Build Status

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqla_paranoid3-0.1.4.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

sqla_paranoid3-0.1.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

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

Hashes for sqla_paranoid3-0.1.4.tar.gz
Algorithm Hash digest
SHA256 354bc6a3d9c4af60b091c783e986784bd870c1f64c5dcc0679a661304e72ccce
MD5 7142465a6f2ff7ee1edd559142b0786f
BLAKE2b-256 aba7b4e5eab424d6fc1e75466a7c6a03689c4a9cc0b87910444b039aa69fed8b

See more details on using hashes here.

File details

Details for the file sqla_paranoid3-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sqla_paranoid3-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 14b979fdcf5d6e683df4148cca59da377a8b13e28785c069fef2e755cf93ef05
MD5 eb4a67bf370d4568c1f7daa1d3b7b602
BLAKE2b-256 f487d6eae9dce00dd9517e9aa2c1b3fbc606ee8016f319a6c84ca71bdcc0037b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page