2

I am trying to migrate my database:

E:\PhytonProgects\natarelke>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection       'default'
    HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL,     such as data truncation upon insertion, by escalating warnings into errors. It   is strongly recommended
you activate it. See: 
https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, catalog, contenttypes, main, ordering,     registration, sessions, users
Running migrations:
Rendering model states... DONE
Applying catalog.0002_auto_20170219_2146...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)

File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line   367, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 356, in execute
output = self.handle(*args, **options)

File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial

File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)

File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)

File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
 File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
 File "C:\Python27\lib\site-packages\django\db\backends\mysql\schema.py", line 43, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
 File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 409, in add_field
self.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
cursor.execute(sql, params)
 File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1060, "Duplicate column name 'user_id'")

When I try to run python manage.py migrate I got errors shown above. Can anybody help me fix them?

11
  • The error says "Duplicate column name 'user_id'". Do you already have tables in your database? Commented Feb 19, 2017 at 20:03
  • yeah i have it., but i don't whant drop it coz i have inserted informations Commented Feb 19, 2017 at 20:05
  • Does your current database schema match your migrations (or some version of your migrations)? Commented Feb 19, 2017 at 20:09
  • how i can check it? before do python. manage.py migrate i remove all mirgate files and than do python manage.py makemigrations Commented Feb 19, 2017 at 20:11
  • Where did your existing tables come from? Migrations only work from a known state, and by removing all migration files and generating new ones you've gotten out of sync… Commented Feb 19, 2017 at 20:20

3 Answers 3

1

It's a simple issue which I also faced. It's nothing big just an issue of migrate command. Usually, when you make new models or new tables, you re-run old migrations along with the newly created ones. Of which sometimes the Django confuses I suppose over here.

Easiest solution what I found was.

  1. 1st empty your recycle bin.
  2. then go to your migrations folder or folders( in case you have more than one app)
  3. temporarily delete ( send all migration files such as 0001_initial.py 0002_auto_20170621_1006 etc. all of them to recycle bin.

  4. Then re-run the commands python manage.py makemigrations and python manage.py migrate

*step 3: is temporary del. here you can also go back to recycle bin easily restore it in one click. (so del is safe)

Sign up to request clarification or add additional context in comments.

Comments

1

I want to tell you my case. This only works if you be patient and you are sure about the changes. Django 2.1

{% for crash in crashes %}

  1. python manage.py makemigrations

  2. Locate the conflict file (In the question catalog.0002_auto_20170219_2146.py or something)

  3. Remove (copy in other file or don't close the file) temporally the columns that already exists into the database . (This is what you must be patient :/ ).

{% endfor %}

  1. python manage.py migrate

  2. Undo the change in files.

That's all, works for me. Moral: if you found an conflict with migrate, you should fix it, dont remove all files.

Comments

0

There is a problem either in your DB, or your migrations, or both. If this is a new project, deleting the DB and all migrations, and recreating them with makemigrations will probably get you out of trouble.

To save your data, you can try reverting to a models.py that matches your DB, and use dumpdata to export your data to a JSON file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.