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

  • Home
  • SEARCH
  • 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 6002483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:59:06+00:00 2026-05-23T00:59:06+00:00

My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,

  • 0

My django model looks like this:

class Entity(models.Model):
    name = models.CharField(max_length=40)
    examples = models.ManyToManyField(Example, blank=True)
    tokens = models.ManyToManyField(Token, blank=True, null=True)

I want to enforce uniqueness among the tokens, i.e. if there is already an entity with tokens [‘a’, ‘b’, ‘c’] I do not want to add another one with [‘a’, ‘b’, ‘c’]. However an entity with tokens [‘a’, ‘b’, ‘c’, ‘d’] or [‘a’, ‘b’] is a different set and should be added.

In the case where an entity with the specific set is already found, I want to add the new examples that have been discovered to that ManyToMany. The old name can stay.

Currently, I run a query to get an existing Entity with the exact token set in question (this is itself a challenge in django), then if it is found I update it with the new example. The problem is that this runs in multiple processes on multiple servers, so there is a race condition between checking whether a matching entity exists, and creating a new one, if it was not found to. This could result in entities with duplicate token sets being created.

One solution I came up with is to use an explicit through model for the ManyToMany, and override the save method for the through model to create a hash of the token set and include it in the through model itself on a column with a unique constraint. I think this would work but it doesn’t seem like a particularly elegant solution.

I would also imagine that this problem is somewhat common in SQL land with mapping tables in the case where you want a unique set – perhaps there is a well known solution here? I am certainly open to using raw sql.

Thanks in advance for your help!

  • 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-05-23T00:59:06+00:00Added an answer on May 23, 2026 at 12:59 am

    I’m not entirely sure this method addresses your problem, but here goes.

    A friend who works with Big insurance company databases was looking for a way of rapidly detecting exact dupes across multiple databases based on a subset of columns. I suggested taking the set of columns that he was checking against, run them through some sort of canonicalifier™, computing an MD5 digest, and saving that as an additional, indexed column for the table. Sucker ran like greased lightning.

    So maybe you can do something similar. It doesn’t address your race condition (nothing I can think of does), but it could significantly simplify the dupe-check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My model looks like this: class Staff(models.Model): StaffNumber = models.CharField(max_length=20,primary_key=True) NameFirst = models.CharField(max_length=30,blank=True,null=True) NameLast
I have a model that looks like this: class Category(models.Model): name = models.CharField(max_length=60) class
I have a Django model that looks something like this: class Person(models.Model): name =
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a Django model that looks like this. class Solution(models.Model): ''' Represents a
I have a Django application. One of my models looks like this: class MyModel(models.Model):
I have a django site. a snippet of myapp/models.py looks like this: class Continent(models.Model):
I have a Django model which looks like this: class MyModel(models.Model): parent = models.ForeignKey(ParentModel)
I have a model which looks like this: class MyModel(models.Model) value = models.DecimalField() date
I have a model, Rating , which looks something like this: class Rating(models.Model): upvotes

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.