django error on migration: "There is no unique constraint matching given keys for referenced table
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Realization
--
Chapters
00:00 Django Error On Migration: &Quot;There Is No Unique Constraint Matching Given Keys For Referenced Ta
01:01 Answer 1 Score 11
01:16 Answer 2 Score 7
01:29 Accepted Answer Score 4
02:14 Answer 4 Score 1
02:28 Thank you
--
Full question
https://stackoverflow.com/questions/4223...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #postgis
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Realization
--
Chapters
00:00 Django Error On Migration: &Quot;There Is No Unique Constraint Matching Given Keys For Referenced Ta
01:01 Answer 1 Score 11
01:16 Answer 2 Score 7
01:29 Accepted Answer Score 4
02:14 Answer 4 Score 1
02:28 Thank you
--
Full question
https://stackoverflow.com/questions/4223...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #postgis
#avk47
ANSWER 1
Score 12
To solve this, needed to add the unique constraint on the postgres table id myself.
psql <your-database-name>
ALTER TABLE swsite_zoneentity ADD CONSTRAINT zone_unique_id UNIQUE(id);
Like this answer
ANSWER 2
Score 7
This problem appears most times because you copied or created your database from a dump and somewhere the unique constraint on your primary key column(as well as other constraints got lost.
Solution:
Open your DB with pg4admin or any client, Databases>your_database>schema>public>tables>your_table right-click
on the table name,
Choose Properties
Select columns tabs
switch primary key on your pk column
save/exit
run migration again
ACCEPTED ANSWER
Score 4
Codejoy,
When you define a primarykey, it is automatically set as unique.. So, just go by:
class ZoneEntity(models.Model):
zone_number = models.CharField(max_length=100, primary_key=True)
....
class CesiumEntity(models.Model):
...
zone_id = models.ForeignKey('ZoneEntity', null=True, blank=True)
...
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'
- python manage.py. makemigration
s
Migrations for 'module1':
0002_auto_20170214_1503.py:
- Create model CesiumEntity
- Create model ZoneEntity
- Add field zone_id to cesiumentity
- python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, module1, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying module1.0002_auto_20170214_1503... OK
ANSWER 4
Score 1
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.