为什么Django不迁移我修改过的模型?

时间:2019-03-16 11:41:09

标签: python django django-models

嗨,我有一个django模型,如下所示:

from django.db import models

class ClientProfile(models.Model):
    id = models.AutoField(primary_key=True)
    firstname = models.CharField(max_length=100,blank=False)
    lastname = models.CharField(max_length=100,blank=False)
    gender = models.CharField(max_length=6,blank=False)
    birthdate = models.DateField(blank=False)
    nationality = models.CharField(max_length=100,blank=False)
    education = models.CharField(max_length=100,blank=True)
    occupation = models.CharField(max_length=100,blank=True)

我最初使用电子邮件字段创建了此模型。但是后来我决定扣除它。 现在,我在迁移时收到此错误:

django.db.utils.OperationalError: near "AUTOINCREMENT": syntax error

,然后我删除了数据库并再次迁移,并且再次收到相同的消息。我尝试删除我的应用程序视图,然后从头开始(创建新迁移并创建新的超级用户,然后再次添加模型),但仍然遇到相同的错误。我什至尝试回滚到初始迁移,然后再次迁移,但又重新获得了电子邮件字段,新的迁移并没有改变它。我该怎么办?

这些是我的迁移: enter image description here

这是我的堆栈跟踪:

D:\University\UT\Thesis\My Project\App\AdjustServer>python manage.py migrate
Operations to perform:
  Apply all migrations: AdjustRest, admin, auth, contenttypes, sessions
Running migrations:
  Applying AdjustRest.0001_initial... OK
  Applying AdjustRest.0002_clientprofile_nationality... OK
  Applying AdjustRest.0003_auto_20190313_2158... OK
  Applying AdjustRest.0004_auto_20190313_2205...Traceback (most recent call last):
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 136, in alter_field
    super().alter_field(model, old_field, new_field, strict=strict)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 523, in alter_field
    old_db_params, new_db_params, strict)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 333, in _alter_field
    self._remake_table(model, alter_field=(old_field, new_field))
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 266, in _remake_table
    self.create_model(temp_model)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 312, in create_model
    self.execute(sql, params or None)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 133, in execute
    cursor.execute(sql, params)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "AUTOINCREMENT": syntax error

AdjustRest.0004_auto_20190313_2205:

# Generated by Django 2.1.7 on 2019-03-13 18:35

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('AdjustRest', '0003_auto_20190313_2158'),
    ]

    operations = [
        migrations.AlterField(
            model_name='clientprofile',
            name='email',
            field=models.EmailField(max_length=254, primary_key=True, serialize=False),
        ),
        migrations.AlterField(
            model_name='clientprofile',
            name='id',
            field=models.IntegerField(default=1),
        ),
    ]

1 个答案:

答案 0 :(得分:1)

尝试不使用id = models.AutoField(primary_key=True),这是多余的,因为如果您未指定其他主键,Django会自动分配一个名为id的主键。

如果仍然失败,请显示迁移文件中的代码。