Skip to content
Advertisement

django error on migration: “There is no unique constraint matching given keys for referenced table

So I have seen that a lot of these kinds of questions have popped up (few answered) and none in a Django aspect that I saw. I am confused why I am getting the error, I am guessing i am missing something on my field decorator or what not in my model definition. Here are the two models… (one abbreviated). I thought I did everything right with unique and primary key set to true in the one table that the foreign key gives reference to but upon migrate I get this error:

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

Edit up dated the code …

JavaScript

Advertisement

Answer

Codejoy,

When you define a primarykey, it is automatically set as unique.. So, just go by:

JavaScript

This will automatically bind the PK of ZoneEntity with zone_id!

If the field you are trying to make the relation IS NOT the primary key, then you can add unique=True and to_field='foo'

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement