On Tue, May 14, 2013 at 12:20 PM, Giorgos Kontogiorgakis
<shortgeorge...@yahoo.com> wrote:
> Hi,
>
> i have this model
>
> class Linkmap(models.Model):
>     vpathid = models.IntegerField(db_column='VPathID') # Field name made
> lowercase.
>     interfacein = models.CharField(max_length=20L, db_column='InterfaceIn')
> # Field name made lowercase.
> #Every Link belongs to a LinkMap
>     #links = models.ForeignKey(Links)
>     interfaceout = models.CharField(max_length=20L,
> db_column='InterfaceOut') # Field name made lowercase.
> #Every Vpath composes a LinkMap
>     vpaths = models.ForeignKey(Vpaths)
>     hopcounter = models.BigIntegerField(null=True, db_column='HopCounter',
> blank=True) # Field name made lowercase.
>     def __unicode__(self):
>         return 'VPathID %s     ,    HopCounter %s' %
> (self.vpathid,self.hopcounter)
>     class Meta:
>         db_table = 'LinkMap'
>
>
> when i uncommented links = models.ForeignKey(Links)
>
> and tried to migrate i got this exeption:
>
> root@Roronoa:/home/roronoa/Desktop/webinterface/mysite# ./manage.py migrate
> myapp
> Running migrations for myapp:
>  - Migrating forwards to 0048_auto__add_field_linkmap_links.
>  > myapp:0048_auto__add_field_linkmap_links
> FailedDryRun:  ! Error found during dry run of
> '0048_auto__add_field_linkmap_links'! Aborting.
> Traceback (most recent call last):
>   File
> "/usr/local/lib/python2.7/dist-packages/South-0.7.6-py2.7.egg/south/migration/migrators.py",
> line 175, in _run_migration
>     migration_function()
>   File
> "/usr/local/lib/python2.7/dist-packages/South-0.7.6-py2.7.egg/south/migration/migrators.py",
> line 57, in <lambda>
>     return (lambda: direction(orm))
>   File
> "/home/roronoa/Desktop/webinterface/mysite/myapp/migrations/0048_auto__add_field_linkmap_links.py",
> line 14, in forwards
>     keep_default=False)
>   File
> "/usr/local/lib/python2.7/dist-packages/South-0.7.6-py2.7.egg/south/db/generic.py",
> line 44, in _cache_clear
>     return func(self, table, *args, **opts)
>   File
> "/usr/local/lib/python2.7/dist-packages/South-0.7.6-py2.7.egg/south/db/generic.py",
> line 402, in add_column
>     sql = self.column_sql(table_name, name, field)
>   File
> "/usr/local/lib/python2.7/dist-packages/South-0.7.6-py2.7.egg/south/db/generic.py",
> line 688, in column_sql
>     default = field.get_db_prep_save(default,
> connection=self._get_connection())
>   File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py",
> line 1047, in get_db_prep_save
>     connection=connection)
>   File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py",
> line 304, in get_db_prep_save
>     prepared=False)
>   File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py",
> line 549, in get_db_prep_value
>     value = connection.ops.validate_autopk_value(value)
>   File
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py",
> line 288, in validate_autopk_value
>     raise ValueError('The database backend does not accept 0 as a '
> ValueError: The database backend does not accept 0 as a value for AutoField.
>
>
>
> I can't do nothing as many tries i did!I not that experienced in South and
> Django,just started to use them!Any help would be appriciated!
>

You wrote the answer in your code*:

     #Every Link belongs to a LinkMap
     links = models.ForeignKey(Links)

When you run the migration, it has no possible value to give the
existing LinkMaps, and the database complains.

If you instead initially define the foreign key with null=True,
blank=True, then the migration can complete - it knows what value to
use, NULL. You can then add a datamigration to add in the necessary
data, so that none of the rows have NULL as a value, and then finally
a second schema migration to take away the null/blank constraints.

Cheers

Tom

* Although your logic is a little off, by putting the foreign key on
the LinkMap, you are saying each LinkMap has exactly one Link. I think
the foreign key should be on Link, so that each Link has exactly one
LinkMap

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to