简体   繁体   English

Django 表已经存在

[英]Django Table already exist

Here is my Django Migration file.这是我的 Django 迁移文件。 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')),

try python manage.py migrate your_app --fake .尝试python manage.py migrate your_app --fake This post talks about it.这篇文章谈到了它。 Django South - table already exists . Django South-table 已经存在

This question is already answered here这个问题已经在这里回答

You should run this:你应该运行这个:

python manage.py migrate <appname> --fake

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

temporary solution may be to comment the creation of existing table(tickets_duration).临时解决方案可能是评论现有表(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')),
        #    ],
        #),
        ....
        ....

version:-Django 3.X版本:-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 ,如果它不起作用,那么查看迁移文件夹,您会发现您在 models.py 中所做的一些更改丢失了,但 Django 以某种方式无法捕获,因此在那里找到它并再次进行一些更改(甚至一个小)到该模型字段,然后使用,

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

It is an inconsistent situation of Database.这是数据库不一致的情况。

You may do the followings:您可以执行以下操作:

  1. Comment out the code for your last added model.注释掉您上次添加的 model 的代码。
  2. Run makemigrations and then migrate --fake, to get the record sync at present situation.运行 makemigrations 然后 migrate --fake,获取当前情况下的记录同步。
  3. Now, uncomment your last added model, and run makemigrations again;现在,取消注释您最后添加的 model,然后再次运行 makemigrations;

It's probably because you're using sqlite3 and table names in sqlite3 are case insensitive .这可能是因为您使用的是 sqlite3 并且sqlite3 中的表名不区分大小写 Try running this command and check if there's two tables are created.尝试运行此命令并检查是否创建了两个表。 Root cause is your ManyToMany field on the Tickets table.根本原因是Tickets表上的ManyToMany字段。

python manage.py sqlmigrate <APP NAME> <MIGRATION ID>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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