Django South - Create Not Null ForeignKey -


i have model

class mystery(models.model):     first = models.charfield(max_length=256)     second = models.charfield(max_length=256)     third = models.charfield(max_length=256)     player = models.foreignkey(player) 

i added player foreignkey when try migrate using south seems can't create whith null=false. have message :

the field 'mystery.player' not have default specified, yet not null. since adding field, must specify default value use existing rows. to:
1. quit now, , add default field in models.py
2. specify one-off value use existing columns now

i use command :

manage.py schemamigration myapp --auto

thanks lot !

another option create data migration before adding foreignkey, in create new player instance specific id. sure that id not exist in database.

1.create data migration file

$ ./manage.py datamigration myapp add_player created 00xx_add_player.py 

2.edit forwards , backwards methods of file:

def forwards(self, orm):     orm['myapp.player'].objects.create(name=u'very misterious player', id=34)  def backwards(self, orm):     # haven't tested 1 yet     orm['myapp.player'].objects.filter(id=34).delete() 

3.add foreignkey mistery class , migrate schema again. ask default value data migration id, in example, 34.

 $ ./manage.py schemamigration --auto myapp  ? field 'mistery.player' not have default specified, yet not null.  ? since adding field, must specify default  ? value use existing rows. to:  ?  1. quit now, , add default field in models.py  ?  2. specify one-off value use existing columns  ? please select choice: 2  ? please enter python code one-off default value.  ? datetime module available, can e.g. datetime.date.today()  >>> 34  + added field player on myapp.mistery created 0010_auto__add_field_mistery_player.py. can apply migration with: ./manage.py migrate myapp 

4.finally run migrate command , execute migrations in sequential order, inserting new player , updating mistery rows reference new player.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -