Your Ad Here

Friday, January 6, 2012

Re: dumpdata and self referencing foreign keys

On Fri, Jan 06, 2012 at 07:47:45AM -0800, Michael Elkins wrote:
>My problem is that "django-admin dumpdata" does not sort the rows such
>that it avoids forward references for the foriegn key values, which is
>a problem since I'm using MySQL InnoDB tables. I see that this
>problem seems to be fixed in the devel tree
>(https://code.djangoproject.com/ticket/3615), but I just wanted to
>make sure there wasn't something I could easily do in the interim.
>
>Since I know there are no circular references, I can write a script to
>postprocess the dumpdata output and reorder to avoid forward
>references.

For future reference, I found a couple of interesting things:

If the model has "ordering = ('id',)" in its Meta, forward
references will be avoided because the objects will be listed in
the order they were created
http://www.pragmar.com/2010/01/16/django-dumpdataloaddata-import-errors-on-recursive-model/

The django-data-tools app
(http://pypi.python.org/pypi/django-data-tools/0.1) enhances the
dumpdata command in django-admin to deal with sorting tables by
dependencies, including self-references.

I ended up creating a script using Django's serialization directly
to create a dump of my table that can be imported with loaddata:

from django.core import serializers
import models

print serlializers.serialize('json', models.Foo.objects.all().ordered_by('id'), use_natural_keys=True)

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

0 comments:

Post a Comment