Django迁移不会更新我的数据库

时间:2016-02-16 13:38:08

标签: django django-migrations

从现有数据库开始,我想更新它的架构。我改变了我的一个模型,然后运行

python manage.py makemigrations myapp

创建迁移,正确定义新表和列。样本输出:

Migrations for 'myapp':
  0001_initial.py:
    - Create model Foo
    - Add field Bar
    - ...

然后我应用迁移:

python manage.py migrate --database mydb

我得到了这个输出:

Operations to perform:
  Apply all migrations: admin, myapp, contenttypes, sessions, auth
Running migrations:
  Rendering model states... DONE
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying auth.0007_alter_validators_add_error_messages... OK

似乎有效。 Git告诉我sqlite文件对应于" mydb"已经改变。但是,它不是将我的新表和列放在里面,而是删除内容,留下带有旧模式的空表。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果您有未应用迁移的旧数据库,则应创建'---------TIMER MACRO--------' 'PURPOSE: Determine how many seconds it took for code to completely run 'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault Dim StartTime As Double Dim SecondsElapsed As Double Dim minutesElapsed As Double 'Remember time when macro starts StartTime = Timer '************************************* '------Start to run code--------' '************************************* Your code goes here '************************************* '----------End code----------' '************************************* 'Determine how many seconds code took to run 'If you want in seconds with two decimals, use this line: 'SecondsElapsed = Round(Timer - StartTime, 2) 'If you want with up to 7 decimals, use this line: SecondsElapsed = Timer - StartTime 'Notify user how long the macro took If SecondsElapsed > 60 Then minutesElapsed = Int(SecondsElapsed / 60) SecondsElapsed = Int(SecondsElapsed - (minutesElapsed * 60)) Msgbox "This code ran successfully in " & minutesElapsed & " minutes and " & SecondsElapsed & " seconds", vbInformation Else Msgbox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation End If 而不进行任何更改:

initial migrations

然后运行“假”迁移

python manage.py makemigrations myapp

之后,您可以使用正常迁移。