简体   繁体   中英

Django Foreign Key not recognising primary key in relation

I have two models, Article and ArticlePost . ArticlePost references Article as a foreign key and is in a separate application to Article :

====app1/models.py:======
class Article(models.Model):

     name = models.CharField(max_length=200, primary_key=True)

====app2/models.py======
class ArticlePost(models.Model):

    article = models.ForeignKey(Article, null=False, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    comment = models.TextField(blank=True)

I have run python manage makemigrations which gives the following:

operations = [
migrations.CreateModel(
    name='ArticlePost',
    fields=[
        ('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)),
        ('created_at', models.DateTimeField(auto_now_add=True)),
        ('comment', models.TextField(blank=True)),
        ('article', models.ForeignKey(to='app2.Article')),
    ],
),
]

However when I run python manage migrate I get:

django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "article"

What is strange is that I have another model in app1 which also references article with a foreign key which works perfectly. However in this case it would appear that Django does not know which field is the primary key for Article . The only difference is that ArticlePost is in a different application from Article . I am running Django 1.10. Does anyone have any idea what is causing this and how it might be fixed?

Alternatively if it is just a key issue maybe a solution is to remove the primary_key on Article and use the Django default id instead. In this case, how is best to do this while maintaining the Foreign Key references from other models to Article within app1?

Okay so what you tried to do is absolutely correct. But the problem here is with Django.

When you are setting name as primary_key then according to the official documentation -> https://docs.djangoproject.com/ja/1.9/ref/models/fields/#django.db.models.Field.unique

primary_key=True implies null=False and unique=True.

So technically, you do have a unique field in your Article model, but after going through few other posts on Stackoverflow,

see this as a reference (go through the comments too) -> Primary key and unique key in django

it seems that there some issue with the primary_key=true . It seems that django only considers it as a primary key but not as unique

So when you use a primary_key=true for name , then Django doesn't creates it own unique auto-incrementing ids for objects. Hence now you don't have any unique id for an object in the model Article due to which you get the error you are getting.

So, simply remove the primary_key=true and let Django use its own auto incrementing unique ids and you should not get that error.

@mohevi, check if you are using MongoDb as database, as mongodb do not supports Foreign key feature. these are supported normally in RDBMS like postgresql, MySQL etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM