0

I have a django model called Contexts which is essentially a group that has a list of users.I have another model called UserProperty which has some user information.

class Contexts(models.Model):
    context_name = models.CharField(max_length=50)
    context_description = models.TextField()
    users = models.ManyToManyField(UserProperty , related_name='context_users')

def getUserImagePath(instance,filename):
    return "/static/images/%s_%s"% (str(time()).replace('.','_'),filename)

class UserProperty(models.Model):
    username = models.CharField(max_length=255, null=False)
    pic = models.ImageField(upload_to=getUserImagePath, default="/static/images/user.png" ,null=True, blank=True)
    org = models.CharField(max_length=255, null=True)

This works fine in my local system when I run a migration using django manage.py.But in the server for some reason, I cannot run migrations and I have to define the models manually in postgres (the server uses django _ postgres).So this is what I did

CREATE TABLE webhook_contexts(id integer, context_name character varying(100), context_description character varying(100), users text [], FOREIGN KEY (users) REFERENCES webhook_userproperty);

I am not sure exactly how to replicate this in postgres.For users field in Contexts model, I did models.ManyToManyField(UserProperty , related_name='context_users') so I assume the users must be an array in postgres that contains the fields of the referenced table UserProperty namely username, pic, org.So I do

.., users text [], FOREIGN KEY (users) REFERENCES webhook_userproperty)

But I get this error

ERROR:  there is no primary key for referenced table "webhook_userproperty"

But I did define an id for the webhook_userproperty which django treats as a primary key.Then why am I getting this error?Is my table definition wrong?

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
  • A many to many is not a specific field: it generates an *extra* table, a table with two foreign keys: one to the "origin" of the m2m, and one to the "target" of the m2m. – Willem Van Onsem Aug 12 '18 at 16:02
  • M2M fields are a separate table with two foreign keys. But rather than trying to do this manually, you would be better off diagnosing what is wrong with running migrations. – Daniel Roseman Aug 12 '18 at 16:03
  • @WillemVanOnsem oh I see.Can you show me how it might look like for my scenario?I am trying to follow this stackoverflow question https://stackoverflow.com/questions/9789736/how-to-implement-a-many-to-many-relationship-in-postgresql but I can't yet figure out how do I implement it in my case. – Souvik Ray Aug 12 '18 at 16:20
  • At the first look, it seems like a more of a django migrations problem. Just few question, do you have migrations files on production? E.g: did you ran python manage.py makemigrations followed by python manage.py migrate ? Can you paste traceback you get running the command. – Vidya Sagar Aug 12 '18 at 16:20
  • @VidyaSagar hey actually this server belongs to my company and there is some issue with running the migrations.So we are defining tables manually for the corresponding models for sometime now. – Souvik Ray Aug 12 '18 at 16:23
  • @SouvikRay: can you share the relevant migration files, and the (full) error you get when you run the migrations. – Willem Van Onsem Aug 17 '18 at 10:54

0 Answers0