Django postgresql foreign key

I have models.py

from django.db import models from django.contrib.auth.models import User class Service(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=800, blank=False) service = models.CharField(max_length=2000, blank=False) def __unicode__(self): return self.name class ServiceCheck(models.Model): service = models.ForeignKey(Service) check_status = models.CharField(max_length=80) check_time = models.DateTimeField() 

When I run syncdb in postgresql, it ends up with an error:

 ~/dev-md> sudo python manage.py syncdb Creating tables ... Creating table monitor_service Creating table monitor_servicecheck DatabaseError: Hash/Modulo distribution column does not refer to hash/modulo distribution column in referenced table. 
+6
source share
2 answers

The code looks fine at a glance. I suspect this is a database problem.

Since the max_length provided max_length pretty huge, try decreasing max_length to something <= 255 or use TextField instead of CharField .

Otherwise, try renaming the service field to service and / or ServiceCheck .

+2
source

The problem you are facing is actually related to how Postgres-XC / StormDB (or now Postgres-XL, where I got into this problem) handle table splitting between different datanodes.

The main problem is that the database engine cannot guarantee foreign key constraints or unique constraints. For an old article on the StormDB website about the previous version of Django and Postgres-XC / StormDB, you can do this by setting loose_constraints=true to the database. In the modern version of Django (1.6 or later), the equivalent seems to set db_constraint=False in ForeignKey according to the field docs of the Django model (which I cannot refer directly to, since I lack rep).

Another solution, if you are more concerned about availability rather than performance, is data replication, which means that you will not have a problem because all data data has the same data. I don’t know how to do it directly in Django, but you can change the table creation to use DISTRIBUTE BY REPLICATION , as described in CREATE TABLE docs.

+1
source

All Articles