Python Django迁移没有从makemigrations中获取更改

时间:2017-02-03 16:18:36

标签: python django

我向models.py添加了一个新模型,对应于我需要创建的新SQL表。我添加了一个字段Id,显然它不喜欢,因为python manage.py migrate表示它是一个重复字段(可能是Id字段也会被自动添加或者其他内容)。< / p>

没问题,所以我从模型中移除了该字段并重新运行了python manage.py makemigrations,这正确地报告了Id字段已被删除。我想重新运行python manage.py migrate,但它仍然会出现重复Id字段的相同错误...

来自models.py

class TII(models.Model):
    Person_bnr_id = models.CharField(max_length = 128, unique = True)
    COURSE_IDENTIFICATION = models.CharField(max_length = 128, unique = True)
    Course_start = models.CharField(max_length = 128, unique = True)
    Course_end = models.CharField(max_length = 128, unique = True)
    TurnItIn_W01 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W02 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W03 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W04 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W05 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W06 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W07 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W08 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W09 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W10 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W11 = models.CharField(max_length = 128, unique = True)
    TurnItIn_W12 = models.CharField(max_length = 128, unique = True)

来自python manage.py migrate

(P27_D19) [root@mdbiawebappdev1 loeuk]# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, studentprogress, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying studentprogress.0100_tii...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/srv/P27_D19/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/srv/P27_D19/lib/python2.7/site-packages/sql_server/pyodbc/schema.py", line 535, in create_model
    self.execute(sql, params or None)
  File "/srv/P27_D19/lib/python2.7/site-packages/sql_server/pyodbc/schema.py", line 584, in execute
    cursor.execute(sql, params)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/srv/P27_D19/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/srv/P27_D19/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 537, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S21', "[42S21] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Column names in each table must be unique. Column name 'Id' in table 'studentprogress_tii' is specified more than once. (2705) (SQLExecDirectW)")

以下是showmigrations的输出:

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
studentprogress
 [X] 0001_initial
 [X] 0002_delete_aa
 [X] 0003_advisor
 [X] 0004_student
 [X] 0005_delete_student
 [X] 0006_student
 [X] 0007_staff
 [X] 0008_remove_staff_mgrid
 [X] 0009_auto_20160505_0909
 [X] 0010_delete_generaluser
 [X] 0011_generaluser
 [X] 0012_remove_generaluser_institution
 [X] 0013_delete_generaluser
 [X] 0014_generaluser
 [X] 0015_delete_generaluser
 [X] 0016_generaluser
 [X] 0017_delete_student
 [X] 0018_delete_generaluser
 [X] 0019_generaluser
 [X] 0020_delete_generaluser
 [X] 0021_generaluser
 [X] 0022_student
 [X] 0023_delete_student
 [X] 0024_student
 [X] 0025_studenthold
 [X] 0026_studentsro
 [X] 0027_delete_studentsro
 [X] 0028_studentsro
 [X] 0029_delete_studentsro
 [X] 0030_studentprep
 [X] 0031_student_program_end_date
 [X] 0032_studentfirstmodule
 [X] 0033_studentpaymentdetail_studentpaymentsummary
 [X] 0034_auto_20160615_1243
 [X] 0035_delete_studentpaymentdetail
 [X] 0036_studentpaymentdetail
 [X] 0037_delete_studentpaymentdetail
 [X] 0038_studentpaymentdetail
 [X] 0039_auto_20160615_1310
 [X] 0040_delete_studentpaymentdetail
 [X] 0041_studentpaymentdetail
 [X] 0042_delete_studentpaymentdetail
 [X] 0043_studentpaymentdetail
 [X] 0044_delete_studentpaymentdetail
 [X] 0045_studentpaymentdetail
 [X] 0046_remove_studentpaymentdetail_effective_due_date
 [X] 0047_studentpaymentdetail_effective_due_date
 [X] 0048_auto_20160615_2307
 [X] 0049_auto_20160615_2309
 [X] 0050_studentsummary
 [X] 0051_delete_studentfirstmodule
 [X] 0052_studentfirstmodule
 [X] 0053_delete_studentfirstmodule
 [X] 0054_studentfirstmodule
 [X] 0055_delete_studentfirstmodule
 [X] 0056_studentfm_uol_studentfm_uor
 [X] 0057_delete_studentfm_uol
 [X] 0058_studentfm_uol
 [X] 0059_delete_studentsummary
 [X] 0060_studentsummary
 [X] 0061_advisorportfolio
 [X] 0062_delete_advisorportfolio
 [X] 0063_advisorportfolio
 [X] 0064_auto_20160712_1525
 [X] 0065_advisor_advisor_name
 [X] 0066_auto_20160713_1614
 [X] 0067_auto_20160713_1616
 [X] 0068_remove_advisor_institution
 [X] 0069_auto_20160713_1620
 [X] 0070_advisor_location
 [X] 0071_auto_20160713_1624
 [X] 0072_auto_20160715_1437
 [X] 0073_studentprep
 [X] 0074_moduleprogress_onboarding
 [X] 0075_auto_20160722_1422
 [X] 0076_studentmoduleprogress_studentonboarding
 [X] 0077_delete_studentmoduleprogress
 [X] 0078_studentmoduleprogress
 [X] 0079_delete_studentonboarding
 [X] 0080_studentonboarding
 [X] 0081_delete_studentonboarding
 [X] 0082_studentonboarding
 [X] 0083_delete_studentpaymentsummary
 [X] 0084_studentpaymentsummary
 [X] 0085_delete_studentpaymentsummary
 [X] 0086_studentpaymentsummary
 [X] 0087_delete_studentpaymentsummary
 [X] 0088_studentpaymentsummary
 [X] 0089_auto_20160905_1827
 [X] 0090_auto_20160905_1828
 [X] 0091_auto_20160905_1828
 [X] 0092_auto_20160905_1829
 [X] 0093_auto_20160905_1829
 [X] 0094_auto_20160906_1112
 [X] 0095_remove_student_referrer_email
 [X] 0096_auto_20160906_1114
 [X] 0097_student_referrer_first_name
 [X] 0098_student_referrer_last_name
 [X] 0099_student_referrer_email
 [ ] 0100_tii
 [ ] 0101_remove_tii_id

1 个答案:

答案 0 :(得分:3)

showmigrations输出显示尚未应用0100_tii迁移。

[X] 0099_student_referrer_email
[ ] 0100_tii
[ ] 0101_remove_tii_id

因此,我会删除0100_tii0101_remove_tii_id文件。然后你可以重新运行makemigrations。如果0100_tii所做的只是添加了ID字段,那么在您对模型进行进一步更改之前,可能不需要运行makemigrations