'Optimizing unique_together in Django Models
I have a model containing an FK and a CharField, and this pair should be unique:
class Map(models.Model):
uuid = models.UUIDField(unique=True, default=uuid4, editable=False, db_index=True)
user = models.ForeignKey(User, related_name="uploaded_map", on_delete=models.CASCADE)
workspace = models.ForeignKey(Workspace, on_delete=models.CASCADE)
c_layer = models.CharField(max_length=40, blank=False)
class Meta:
unique_together = ["workspace", "c_layer"]
The project uses PostgreSQL, and I am a bit worried that this constraint will create heavy indexes because of the 40 chars limit for c_layer, but I'm not sure this is really something that will impact performance.
Would index a sha1 hash version of this field be less heavy for the DB?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
