2016-06-14 46 views
0

我想驗證django 1.8中是否存在使用sql lite進行makemigrations的錯誤,或者我做錯了什麼。django中的makemigrations是否因爲某種原因重新創建現有的表?

  1. 我刪除了我的數據庫並刪除了所有遷移文件夾。我跑
python manage.py makemigrations 
python manage.py migrate 

DB被創建沒有問題。

2.我必須修改現有模式在應用程序中的一個(應用ABC) 我執行我的變化,然後再次運行

python manage.py makemigrations 

3.it沒有發現任何變化

然後我再次運行同樣的事情,但隨着應用程序的名稱

python manage.py makemigrations abc 

4.它在遷移時做了一些更新,我相信它會重新創建所有表格,而不僅僅是我的變化!!!!

5.Then我執行

python manage.py migrate

並得到錯誤的表已經存在。

這是在Django框架中的錯誤,或者我做錯了什麼,並有一個原因,爲什麼它的行爲是這樣嗎?

從我的外殼複製粘貼從第2步開始:

(mrp) C:\Users\I812624\dev\mrp\src>python manage.py makemigrations 
No changes detected 

(mrp) C:\Users\I812624\dev\mrp\src>python manage.py makemigrations purchase 
Migrations for 'purchase': 
    0001_initial.py: 
    - Create model PO 
    - Create model POmaterial 

(mrp) C:\Users\I812624\dev\mrp\src>python manage.py migrate 
Operations to perform: 
    Synchronize unmigrated apps: customer, manufacture, product, django_filters, a 
utofixture, staticfiles, messages, smart_selects, watson, sales, item, django_co 
untries, mptt, inventory, django_select2, production, main, crispy_forms 
    Apply all migrations: purchase, vendor, sessions, admin, sites, flatpages, con 
tenttypes, auth, registration 
Synchronizing apps without migrations: 
    Creating tables... 
    Running deferred SQL... 
    Installing custom SQL... 
Running migrations: 
    Rendering model states... DONE 
    Applying purchase.0001_initial...Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\__init 
__.py", line 338, in execute_from_command_line 
    utility.execute() 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\__init 
__.py", line 330, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\base.p 
y", line 390, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\base.p 
y", line 441, in execute 
    output = self.handle(*args, **options) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\comman 
ds\migrate.py", line 221, in handle 
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\executor 
.py", line 110, in migrate 
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=f 
ake_initial) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\executor 
.py", line 147, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\migratio 
n.py", line 115, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, projec 
t_state) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\operatio 
ns\models.py", line 59, in database_forwards 
    schema_editor.create_model(model) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\base\schem 
a.py", line 282, in create_model 
    self.execute(sql, params or None) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\base\schem 
a.py", line 107, in execute 
    cursor.execute(sql, params) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\utils.py", 
line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\utils.py", 
line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\utils.py", line 97, 
in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\utils.py", 
line 62, in execute 
    return self.cursor.execute(sql) 
    File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\sqlite3\ba 
se.py", line 316, in execute 
    return Database.Cursor.execute(self, query) 
django.db.utils.OperationalError: table "purchase_po" already exists 
+0

請粘貼完整的錯誤追溯 – ravigadila

+0

我粘貼了我的shell的所有內容。 –

+1

通過查看您的日誌。 '0001_initial.py'。在第3步中爲您的應用程序創建實際的遷移。你有沒有刪除步驟1中的任何遷移文件?如果數據不重要刪除數據庫並重覆上述步驟一切都會正常工作。 – MicroPyramid

回答

0

要回答我的問題。

所以我想通了。我認爲問題在於我最初刪除了文件夾遷移,而當我運行makemigrations時未指定特定的應用程序,它確實創建了一個數據庫,但它已經創建了包含____init____.py的文件夾makemigrations。

解決方案每當你刪除數據庫和刪除migrations文件夾不管是什麼原因不刪除遷移文件夾,但只有其內容不包括init文件。 或者當您確實刪除文件夾時,運行爲@MicroPyramid建議的每個應用程序單獨進行遷移。

沒有做更深入的調查,它在我看來或是作爲令人困惑的設計行爲或來自Django方面的錯誤。

相關問題