7

Here is my Django Migration file. When I run

python manage.py makemigrations/migrate 

I get this error.

Error:-

    django.db.utils.OperationalError: (1050, "Table 'tickets_duration' already exists")

I have dropped the database and running it but still get the same error.

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Duration',
            fields=[
                ('Id', models.UUIDField(primary_key=True, db_column=b'duration_id', default=uuid.uuid4, serialize=False, editable=False)),
                ('duration', models.CharField(max_length=200, db_column=b'duration')),
            ],
        ),
        migrations.CreateModel(
            name='ErrorCount',
            fields=[
                ('Id', models.UUIDField(primary_key=True, db_column=b'error_id', default=uuid.uuid4, serialize=False, editable=False)),
                ('error', models.CharField(max_length=200, db_column=b'error')),
            ],
        ),
        migrations.CreateModel(
            name='OutageCaused',
            fields=[
                ('Id', models.UUIDField(primary_key=True, db_column=b'error_id', default=uuid.uuid4, serialize=False, editable=False)),
                ('outage_caused', models.CharField(max_length=200, db_column=b'outage_caused')),
            ],
        ),
        migrations.CreateModel(
            name='Pg',
            fields=[
                ('Id', models.UUIDField(primary_key=True, db_column=b'pg_id', default=uuid.uuid4, serialize=False, editable=False)),
                ('pg_cd', models.CharField(max_length=200, db_column=b'pg_cd')),
            ],
        ),
        migrations.CreateModel(
            name='SystemCaused',
            fields=[
                ('Id', models.UUIDField(primary_key=True, db_column=b'error_id', default=uuid.uuid4, serialize=False, editable=False)),
                ('system_caused', models.CharField(max_length=200, db_column=b'system_caused')),
            ],
        ),
        migrations.CreateModel(
            name='Tickets',
            fields=[
                ('ticket_num', models.CharField(max_length=100, serialize=False, primary_key=True, db_column=b'ticket_id')),
                ('created_dt', models.DateTimeField(db_column=b'created_dt')),
                ('ticket_type', models.CharField(max_length=20, db_column=b'ticket_type')),
                ('addt_notes', models.CharField(max_length=1000, db_column=b'addt_notes')),
                ('row_create_ts', models.DateTimeField(default=datetime.datetime(2016, 2, 29, 16, 58, 31, 584733))),
                ('row_end_ts', models.DateTimeField(default=b'9999-12-31 00:00:00.00000-00', db_column=b'row_end_ts')),
                ('duration', models.ManyToManyField(to='tickets.Duration')),
                ('error_count', models.ManyToManyField(to='tickets.ErrorCount')),
                ('outage_caused', models.ManyToManyField(to='tickets.OutageCaused')),
Risadinha
  • 16,058
  • 2
  • 88
  • 91
user1050619
  • 19,822
  • 85
  • 237
  • 413

7 Answers7

13

try python manage.py migrate your_app --fake. This post talks about it. Django South - table already exists.

Community
  • 1
  • 1
bhzag
  • 2,932
  • 7
  • 23
  • 39
5

python manage.py migrate --fake-initial should work for django 2.2

LoneCuriousWolf
  • 620
  • 1
  • 6
  • 16
4

This question is already answered here

You should run this:

python manage.py migrate <appname> --fake

pyjavo
  • 1,598
  • 2
  • 23
  • 41
2

temporary solution may be to comment the creation of existing table(tickets_duration).

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        #migrations.CreateModel(
        #    name='Duration',
        #    fields=[
        #        ('Id', models.UUIDField(primary_key=True, db_column=b'duration_id', default=uuid.uuid4, serialize=False, editable=False)),
        #        ('duration', models.CharField(max_length=200, db_column=b'duration')),
        #    ],
        #),
        ....
        ....
SuperNova
  • 25,512
  • 7
  • 93
  • 64
1

version:-Django 3.X

If above solution doesn't work :

python manage.py migrate <appname> --fake

If it doesn't work then have a look at the migrations folder you will find that there will be some missing changes which u have done in models.py but somehow Django is unable to capture, so find it there and again do some changes (even a small) to that model fields and then use ,

py manage.py makemigrations app_name
py manage.py migrate app_name
or
py manage.py makemigration <appname> --fake

Saikat Mukherjee
  • 500
  • 5
  • 11
0

It is an inconsistent situation of Database.

You may do the followings:

  1. Comment out the code for your last added model.
  2. Run makemigrations and then migrate --fake, to get the record sync at present situation.
  3. Now, uncomment your last added model, and run makemigrations again;
0

I had the same problem when using sqlite3 because of the way Django creates the third join table. From the docs,

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it.

So basically, the third table is created by concatenating the two table names. This doesn't work with sqlite3 because table names in sqlite3 are case insensitive.

The resolution is to add the db_table option when declaring your ManyToManyField.

models.ManyToManyField(to='tickets.Duration', db_table='_Duration')
Keenan
  • 194
  • 1
  • 8