2013-03-27 34 views
1

我正在使用South進行sqlite3數據遷移。我的舊模式有用戶配置以下模型:使用OneToOne鍵進行南數據遷移

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    weekOne = models.OneToOneField(WeekOne) 
    weekTwo = models.OneToOneField(WeekTwo) 
    weekThree = models.OneToOneField(WeekThree) 

但是我有意識添加了一些新的星期,即weekFour,weekFive,weekSix等每週本身就是一個模型,從一個普通的繼承周模型。因此,一個星期模型的基本原型是這樣的:

class WeekOne(Week): 
    name = u'Week One' 
    exercises = ['squats', 'lunges', 'stairDaysCount', 'skipStairs'] 
    # Required benchmarks for given exercises 
    squatBenchmark = 1000 
    lungeBenchmark = 250 
    stairDaysCountBenchmark = 3 

    totalGoals = 4 

我的問題是,我應該把什麼樣的代碼在我的datamigration代碼,這樣我可以用另外的周老填充UserProfile秒。我有這樣的想法:

def forwards(self, orm): 
    for user in orm.UserProfile.objects.all(): 
     user.weekFour = orm.WeekFour() 
     user.weekFive = orm.weekFive() 
     # etc. 

但這似乎並沒有工作。我得到這個錯誤,當我嘗試運行模式遷移:

Migration 'my_app:0002_newWeeks' is marked for no-dry-run 

後來這樣的:

DatabaseError: no such column: my_app_userprofile.weekFour_id 

回答

0

只需在終端運行這個

python manage.py schemamigration myapp --auto 

然後

python manage.py migrate 
+0

如果我的應用程序已經創建了沒有新的模型,這不起作用鍵。也就是說,我創建了一個以1-3周爲關鍵詞的用戶,用這些鍵和那個用戶做了一堆東西,然後在第4-9周加入。我所有的新用戶都將有整個星期,但是舊用戶在4-9周內會有空,而且鍵是'NOT NULL'鍵,所以這會導致錯誤。 – user1427661 2013-03-28 21:24:04

+0

你可以定義一個默認值,這樣你就不會收到錯誤 – catherine 2013-03-29 00:37:26

相關問題