Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting "Error: Unable to serialize database:" when performing dumpdata

For some reason today I cannot dump my database using python manage.py dumpdata or from a link that can download the mysql file.

I tried to use python manage.py dumpdata --traceback and here is the information I have.

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/dumpdata.py", line 114, in handle
    use_natural_keys=use_natural_keys)
  File "/usr/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 91, in serialize
    s.serialize(queryset, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 48, in serialize
    self.handle_fk_field(obj, field)
  File "/usr/local/lib/python2.7/site-packages/django/core/serializers/python.py", line 48, in handle_fk_field
    related = getattr(obj, field.name)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 301, in __get__
    raise self.field.rel.to.DoesNotExist
django.contrib.auth.models.DoesNotExist

It says django.contrib.auth.models.DoesNotExist. I wonder if it has something to do with a Foreign key or something.

models.py

class Client(models.Model):
    name = models.CharField(max_length = 40)
    telephone = models.CharField(max_length = 20)
    website = models.URLField(verify_exists = False)
    fax = models.CharField(max_length = 20)
    email = models.EmailField()
    is_active = models.BooleanField()
    user  = models.ForeignKey(User)
    datetime = models.DateTimeField(default=datetime.now)
    note = models.TextField()
    def __unicode__(self):
        return self.name

From my models.py, the field user, datetime and note were added recently. Now if for a client, any of these fields do not have a value i.e. blank, I will get the error Unable to serialize database.

When I have looked up user, dateime and note in mysql. The table for Client shows user_id, datetime and note to have Null values (which is what I want). Why does it not allow Null values?

+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name      | varchar(40)  | NO   |     | NULL    |                | 
| telephone | varchar(20)  | NO   |     | NULL    |                | 
| website   | varchar(200) | NO   |     | NULL    |                | 
| fax       | varchar(20)  | NO   |     | NULL    |                | 
| email     | varchar(75)  | NO   |     | NULL    |                | 
| is_active | tinyint(1)   | NO   |     | NULL    |                | 
| user_id   | int(11)      | YES  | MUL | NULL    |                | 
| datetime  | datetime     | YES  |     | NULL    |                | 
| note      | longtext     | YES  |     | NULL    |                | 
+-----------+--------------+------+-----+---------+----------------+
like image 484
Shehzad009 Avatar asked Jul 13 '11 14:07

Shehzad009


1 Answers

You need to tell Django that it should allow NULL values on those recently added fields. It looks only at the fields definition, it doesn't fetch the schema from the database.

The self.field.rel.to.DoesNotExist error is most likely raised when a client doesn't have an user associated and client.user is accessed. It's because by default all fields are required. If you change your model definition as shown bellow, the error should go away.

class Client(models.Model):
    # ...
    user  = models.ForeignKey(User, 
                             null=True, blank=True) 
    datetime = models.DateTimeField(default=datetime.now,
                             null=True, blank=True)
    note = models.TextField(null=True, blank=True)
  • null=True allows NULL respectively None values on a field.
  • blank=True allows you to leave the the field empty in a model form (eg. in the admin).

(blank and null are both False by default)

like image 62
Jakub Roztocil Avatar answered Nov 12 '22 12:11

Jakub Roztocil