'django.db.utils.IntegrityError: UNIQUE constraint failed: new__vacancies_company.owner_id error
I try to migrate and an error occurs:
django.db.utils.IntegrityError: UNIQUE constraint failed: new__vacancies_company.owner_id
from django.db import models
from django.contrib.auth.models import User
class Company(models.Model):
name = models.CharField(max_length=64)
location = models.CharField(max_length=64)
logo = models.ImageField(upload_to="MEDIA_COMPANY_IMAGE_DIR")
description = models.TextField()
employee_count = models.IntegerField()
owner = models.OneToOneField(User, on_delete=models.CASCADE, related_name="owner_user")
class Specialty(models.Model):
code = models.CharField(max_length=32)
title = models.CharField(max_length=32)
picture = models.ImageField(upload_to="MEDIA_SPECIALITY_IMAGE_DIR")
class Vacancy(models.Model):
title = models.CharField(max_length=100)
specialty = models.ForeignKey(Specialty, on_delete=models.CASCADE, related_name="vacancies")
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name="vacancies")
skills = models.TextField()
description = models.TextField()
salary_min = models.FloatField()
salary_max = models.FloatField()
published_at = models.DateTimeField()
class Application(models.Model):
written_username = models.CharField(max_length=24)
written_phone = models.CharField(max_length=12)
written_cover_letter = models.CharField(max_length=300)
vacancy = models.ForeignKey(Vacancy, on_delete=models.CASCADE, related_name="applications")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="applications")
python manage.py runserver result:
OperationalError at / no such column: vacancies_company.owner_id
Solution 1:[1]
this happen to me when i try to add new models with foreign keys and the old models already have some data in them. if that is the case:
what i do is :
- comment the new models
- runserver
- delete every data in my models in the admin page
- uncomment the new models
- makemigration and migrate
if it persists
- redo steps and re-create superuser
if still
- delete the database file (sqlite file from directory)
- in you app delete migrations except (init.py) file
- makemigration and migrate
that is usually how i treat it , hope thay'll help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | taha maatof |
