简体   繁体   中英

Why getting this error "django.db.utils.OperationalError: (1050, "Table 'someTable' already exists")"

I am getting error like django.db.utils.OperationalError: (1050, "Table 'someTable' already exists") I want to know reason for getting this type error. I ran the following commands on termial

1. python manage.py makemigration app
2. python manage.py migrate app

When ran migrate then getting the above error. I solve my problem by running

python manage.py migrate --fake app

But I want to know why I am getting this error, and how --fake app solve my problem. Thanks

The table 'someTable' already exists in your database - either because it's been created by a previous call to ./manage.py syncdb or because you created it manually (or you used South before and are switching to Django >= 1.7) - and you obviously didn't have any existing django (non-south) migration, so makemigration thinks the table has to be created (rightly so sonce that will indeed be the case for someone that install your app from scratch).

Using the --fake flag tells migrate command to just record the migration has having been applied without effectively applying it, and that's the whole point of this flag: when your app has already been installed (db tables etc) without migrations and you want to start using migrations.

This problem means that someTable was created without Django migrations or record about migrations in django_migrations was deleted. --fake adds record about migrations in django_migrations without applying actual migrations.

Record in django_migrations contains information about migration: app label, migration name and date when migrations was applied.

I had the same problem. I was using the MySQL database.

With SQLite, you will need to delete the SQLite.db

All I did when using MySQL as database to handle this was...

  1. make migrations python manage.py makemigrations
  2. drop the database from the mysql shell mysql> drop database [database name];
  3. create the database from the mysql shell mysql> create database [database name];
  4. migrate using the python manage.py migrate --run-syncdb command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM