Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:39:01+00:00 2026-06-05T16:39:01+00:00

Say I have a bunch of tables where the objects are marked deleted rather

  • 0

Say I have a bunch of tables where the objects are marked deleted rather than actually deleted. Now, I want to enforce a constraint that there can be only one non-deleted object with a particular set of field values, but I can have multiple deleted objects with the same field values.

class Deletable(models.Model):
    deleted = models.BooleanField(default=False)

    class Meta:
       abstract=True

    def soft_delete(self):
       self.deleted=True
       self.save()

class ConcreteModel(Deletable):
    a = models.IntegerField()
    b = models.IntegerField()

    class Meta:
       #wrong because there may have been some deleted rows
       unique_together=('a', 'b')

What is the best way to enforce the constraint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:39:02+00:00Added an answer on June 5, 2026 at 4:39 pm

    Define your unique constraint across one more field: deleted and your pseudo-unique fields. Then, to represent a soft delete, assign the model’s id to deleted; for undeleted items, assign 0.

    With this approach, for undeleted items, since the deleted field is consistently-valued, the multi-field unique constraint will effectively ignore the value of the deleted and enforce uniqueness for just the pseudo-unique fields; for deleted items, deleted will be taken into account, and since it is unique, the constraint will always be satisified – so any number of models with the same pseudo-unique fields’ values can coexist.

    For example, the following code might be what you’re looking for.

    class Deletable(models.Model):
        deleted = models.IntegerField(default=0)
    
        class Meta:
           abstract=True
    
        def soft_delete(self):
           self.deleted=self.id
           self.save()
    
    class ConcreteModel(Deletable):
        a = models.IntegerField()
        b = models.IntegerField()
    
        class Meta:
           unique_together=('a', 'b', 'deleted')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++, say you have a whole bunch of objects that you want to
Let's say I have a bunch of classes that look something like... class Foo{
Let's say that I have a bunch of class instances that serve different purposes,
Let's say I have some Control that has been disabled. It contains a bunch
Let's say I have a database that has a bunch of stock quotes TableQuotes
Say I have: class Vector3 { float x, y, z; ... bunch of cuntions
Say I have a Telerik MVC Grid, AJAX bound and I want to put
I have a stored procedure that has a bunch of input and output parameters
I have a section of an html page that I want to display in
Let's say I have a list of products. I want to display a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.