lostb1
lostb1

Reputation: 43

Unable to migrate in django

model.py

class Staff(models.Model):
   organization = models.ForeignKey(
        Organization, related_name='staff_organization', on_delete=models.CASCADE, blank=True)
    user = models.OneToOneField(
        User, on_delete=models.CASCADE, blank=True)
    phone = models.CharField(max_length=15, blank=True)
    address = models.CharField(max_length=200, blank=True)

I am trying to add the new field in the database table. II just added the address field. I got following error:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line    
    utility.execute()
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
    cursor.execute(sql, params)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user_email_1c89df09_uniq" already exists

Upvotes: 1

Views: 342

Answers (2)

Shreyash mishra
Shreyash mishra

Reputation: 790

try to run

python manage.py migrate <yourappname> zero

then go into you migration folder and delete all the file except pycache and initpy then again

python manage.py makemigrations
python manage.py migrate

this has to solve your problem

Upvotes: 1

Srijwal R
Srijwal R

Reputation: 573

One option is to delete auth_user_email_1c89df09_uniq constraint from db in any database management system. i did it with pgadmin 2.

Upvotes: 0

Related Questions