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?