2014-09-02 47 views
2

因此,我是Django的新手,我向model.py添加了一個字段,並在Django中創建了問題。在sqlite上南遷移失敗後,我切換到MYSQL

我學會了我需要一個遷移工具,並用南。結果南出現問題與sqlite。所以我爲MYSQL配置了Django設置。我可以將數據添加到數據庫(MYSQL)

我刪除了db.sqlite3文件,但它在syncDB之後回來。

當我運行執行syncdb這樣說的:

Syncing... 
Creating tables ... 
Installing custom SQL ... 
Installing indexes ... 
Installed 0 object(s) from 0 fixture(s) 

Synced: 
> django.contrib.admin 
> django.contrib.auth 
> django.contrib.contenttypes 
> django.contrib.sessions 
> django.contrib.messages 
> django.contrib.staticfiles 
> south 

Not synced (use migrations): 
- accounts 
(use ./manage.py migrate to migrate these) 

所以我的問題是:1。 更改爲MySQL並沒有刪除中期遷移是南失敗的,因爲我希望的那樣。現在我有哪些選擇,以應對這種

  1. 我連做任何事情,我也可以繼續工作,我的應用程序或者是這個月中旬遷移的東西來解決?我問,因爲新的MYSQL DB似乎有我添加的新領域,這就是爲什麼我想遷移的原因。所以你可以看到我的困惑......我的遷移破壞了,但現在使用MYSQL,這些字段沒問題,但是Django仍然認爲即將進行遷移。爲什麼是這樣的,建議是什麼?

感謝

+0

當我運行執行syncdb -all,即移除了「不同步」帳戶並添加到「同步」。但是,如果我運行syncdb帳戶報告爲「未同步」再次。剛剛和FYI,我不知道這意味着什麼,仍然需要建議。 Django新手。 – 2014-09-02 00:58:49

+0

哪個是你的Django版本? – AlvaroAV 2015-05-27 15:12:22

+0

謝謝@Liarez,但我已經從Django的移動上,因爲它是比我更需要。在6個月內沒有使用它。我的應用程序非常小。不管怎麼說,還是要謝謝你。 – 2015-05-27 19:07:09

回答

0

我曾與數據庫&遷移打很多很多很多(多)次&南......我希望這些信息可以幫助您解決您的問題,節省您的辛勤工作

幾個小時

遷移帳戶應用

  • 我有一個問題在這裏,是accounts您的應用程序的名稱?或者它是一個外部應用程序?

accounts應用程序似乎是由南非進行管理,這意味着它不會用命令python manage.py syncdb進行同步,則需要通過自己與遷移是:

# This command generates the migrations file 
python manage.py shchemamigration accounts --initial 
# This command use the migrations file and apply the changes to the DB 
python manage.py migrate accounts 


# --initial is used only in the INITIAL migration 
# If you modify the models on the accounts app and want to migrate again 
# You will need to 
python manage.py shchemamigration accounts --auto 
python manage.py migrate accounts 

留意您遷移

如果選中的文件夾migrations以下的標準結構是yourapp/yourapp/migrations

  • 如果刪除數據庫,記得刪除遷移文件,是更好的,除非你需要保存數據庫的變化
  • 確保您的數據庫和遷移同步重新開始如果它們是同步增加一些對數據庫的修改將很容易

  • 有些時候,雖然使用了南,當你做python manage.py syncdb您的應用程序同步,我將解釋其問題可能來自這裏:

    1. 您的應用程序模型的數據庫命令python manage.py syncdb
    2. 1之後創建的。你做python manage.py schemamigration --initial,它會創建初始遷移現在
    3. ,如果你嘗試做python manage.py migrate yourapp它會失敗因爲表已經創建,所以需要假初始遷移有:

      python manage.py migrate yourapp --fake 
      

      ,從這裏,你可以使用以南沒有問題

如何「重新啓動」數據庫

這個過程將清空數據庫,並刪除所有的遷移,如果您想要保存

有時你會惹遷移一些數據打理,並根據問題,它可以更快地再次刪除數據庫,並啓動遷移:

python manage.py dbshell 

# Next 3 lines inside the MySQL console 
>> drop database yourappdatabase 
>> create database yourappdatabase 
>> quit 

# Delete all the migrations within the migrations folder 
rm yourapp/yourapp/migrations/00* 

python manage.py syncdb 
python manage.py schemamigration yourapp --initial 
python manage.py migrate yourapp 

如何從數據庫

一段時間保存的信息■當你開始一個新的應用程序,你會刪除/創建數據庫多次,並避免對模型一再鍵入數據,它可能是有用的創建與某些型號遷移:

python manage.py dumpdata yourapp.ModelName > file.json 

要重新加載此數據再次,你只需要:

python manage.py loaddata file.json