Django Table已经存在

时间:2016-02-29 17:01:26

标签: python django django-migrations

这是我的Django Migration文件。 我跑的时候

python manage.py makemigrations/migrate 

我收到此错误。

Error:-

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

我删除了数据库并运行它但仍然遇到同样的错误。

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')),

4 个答案:

答案 0 :(得分:5)

尝试python manage.py migrate your_app --fake。这篇文章谈到了它。 Django South - table already exists

答案 1 :(得分:2)

临时解决方案可能是评论现有表的创建(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')),
        #    ],
        #),
        ....
        ....

答案 2 :(得分:1)

此问题已经回答here

你应该运行:

python manage.py migrate <appname> --fake

答案 3 :(得分:0)

python manage.py migrate --fake-initial应该适用于django 2.2