简体   繁体   中英

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...

class ZoneEntity(models.Model):
    zone_number = models.CharField(max_length=100, primary_key=True)
    mpoly = models.PolygonField() #this should grow and shrink for the most representative one...
    objects = models.GeoManager() 
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

class CesiumEntity(models.Model):
    be_number = models.CharField(max_length=100) #the number assigned to a foot print to distinguish
    #zone_id = models.CharField(max_length=100, null=True, blank=True)
    zone_id = models.ForeignKey('ZoneEntity', null=True, blank=True)

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 ...

class ZoneEntity(models.Model):
    zone_number = models.CharField(max_length=100, primary_key=True)
    mpoly = models.PolygonField() #this should grow and shrink for the most representative one...
    objects = models.GeoManager() 
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

class CesiumEntity(models.Model):
    be_number = models.CharField(max_length=100) #the number assigned to a foot print to distinguish
    #zone_id = models.CharField(max_length=100, null=True, blank=True)
    zone_id = models.ForeignKey('ZoneEntity', null=True, blank=True)

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 ...

class ZoneEntity(models.Model):
    zone_number = models.CharField(max_length=100, primary_key=True)
    mpoly = models.PolygonField() #this should grow and shrink for the most representative one...
    objects = models.GeoManager() 
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

class CesiumEntity(models.Model):
    be_number = models.CharField(max_length=100) #the number assigned to a foot print to distinguish
    #zone_id = models.CharField(max_length=100, null=True, blank=True)
    zone_id = models.ForeignKey('ZoneEntity', null=True, blank=True)

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 ...

class ZoneEntity(models.Model):
    zone_number = models.CharField(max_length=100, primary_key=True)
    mpoly = models.PolygonField() #this should grow and shrink for the most representative one...
    objects = models.GeoManager() 
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

class CesiumEntity(models.Model):
    be_number = models.CharField(max_length=100) #the number assigned to a foot print to distinguish
    #zone_id = models.CharField(max_length=100, null=True, blank=True)
    zone_id = models.ForeignKey('ZoneEntity', null=True, blank=True)

I too had same issue while migrating DB from SQLite to PostgreSQL 14.4, even when referenced Foreign key had primary_key=True set.

Deleting the old migrations, solved my issue.

The answer given by Otobong Jerome solved this issue for me. I did exactly what he described (restoring a database from a dump file) and sure enough, the primary was gone! Thanks!

"

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