Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

django.db.utils.IntegrityError: UNIQUE constraint failed: new__vacancies_company.owner_id error

Ask Question

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

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

    If you have had ForeignKey before, and now changed to OneToOneField,

    If you have used python3 manage.py migrate --fake before and now you added an new field to your model with the OneToOneField

    Solution 1.

    Change the OneToOneField back to a ForeignKey

    Solution 2.

    python3 manage.py migrate --fake

    and then go to the admin page and make sure that the owner is unique, meaning that all the users that has a company is owner for only 1 company. If a user is owner for 2 or more companies, then you can delete that company which has an owner with 2 or more companies, or go to Solution 1, since OneToOneField means that you only can have 1 owner which is a User for 1 company. So if a user is owner for 2 companies or more, then it will give you that error.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.