2012-10-19 81 views
1

我有一個空白的MySQL數據庫,我剛剛創建。 south在我的INSTALLED_APPS爲什麼南遷移失敗?

我運行:

$ ./manage.py syncdb 
... 
Creating table some_app_table 

You just installed Django's auth system, which means you don't have any superusers defined. 
... 
Superuser created successfully. 
Installing custom SQL ... 
Installing indexes ... 
Installed 0 object(s) from 0 fixture(s) 

Synced: 
> django.contrib.auth 
> django.contrib.contenttypes 
... 

Not synced (use migrations): 
- myapp 
... 


$ ./manage.py schemamigration myapp --initial 
+ Added model myapp.Model 
... 
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp 


$ ./manage.py migrate myapp 
Running migrations for myapp: 
- Migrating forwards to 0003_initial. 
> skan:0001_initial 
> skan:0002_initial 
FATAL ERROR - The following SQL query failed: CREATE TABLE `myapp_model` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `user_id` integer NULL, `name` varchar(200) NOT NULL); 
The error was: (1050, "Table 'myapp_model' already exists") 

這是怎麼回事?爲什麼不能正確初始化?

+1

'--initial'應該只用於創建初始表;你已經創建了一個表myapp_model。 – geoffspear

+0

如果可能,重新創建數據庫並再次運行 - 初始遷移,而不運行syncdb – Mikael

+0

@Wooble在我運行該命令之前,表不存在,我進行了雙重檢查 – fredley

回答

5

您已經定義了一些遷移:initial(只有在預期的情況下)纔會用於初始遷移。

syncdb輸出說道:

Not synced (use migrations): 
- myapp 

這表明南方工作正常。但是,你這樣做:

$ ./manage.py schemamigration myapp --initial 
+ Added model myapp.Model 
... 
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp 

通知的0003 -prefix:這個(最有可能)表示,目前已經有定義的遷移。這是由你的下一個命令的輸出證實:

$ ./manage.py migrate myapp 
Running migrations for myapp: 
- Migrating forwards to 0003_initial. 
> skan:0001_initial 
> skan:0002_initial 
<snip> 

換句話說,你已經有一對夫婦的initial遷移,其中至少有一個將創建一個表。您的#3遷移再次嘗試,但失敗,因爲表格當然存在。

你需要做的只是使用initial創建你的Django應用程序。只要migrations文件夾包含名爲0001_initial.py的文件,就不需要任何初始遷移了。如果從這個角度改變你的桌子上,用auto調用它,然後遷移:

./manage.py schemamigration myapp --auto 
./manage.py migrate myapp