Reputation: 73195
I am trying to export some data from a MySQL database and import it into a PostgreSQL database. The two models are described below:
class Location(models.Model):
name = models.CharField(max_length=100)
class Item(models.Model):
title = models.CharField(max_length=200)
location = models.ForeignKey(Location)
class Book(Item):
author = models.CharField(max_length=100)
Notice that the Book
model inherits from the Item
model. (Also, I do realize that author
really should be a separate model - but I need something simple to demonstrate the problem.) I first attempt to export the data from the model using the dumpdata
command:
./manage.py dumpdata myapp.location >locations.json ./manage.py dumpdata myapp.item >items.json ./manage.py dumpdata myapp.book >books.json
The JSON in items.json
looks something like this:
{
"fields": {
"title": "Introduction to Programming",
"location": 1
},
"model": "myapp.item",
"pk": 1
}
The JSON in books.json
looks something like this:
{
"fields": {
"author": "Smith, Bob"
},
"model": "myapp.book",
"pk": 1
}
I can import locations.json
and items.json
without issue but as soon as I attempt to import books.json
, I run into the following error:
IntegrityError: Problem installing fixture 'books.json': Could not load myapp.Book(pk=1): null value in column "location_id" violates not-null constraint
Edit: the schema for myapp.books
(according to PostgreSQL itself) is as follows:
# SELECT * FROM myapp_book; item_ptr_id | author -------------+-------- (0 rows)
Upvotes: 2
Views: 471
Reputation: 1930
The books.json file needs to have all the fields of the parent model, in your case 'title' and 'location' fields (and appropriate values of course) need to be added (from the items.json file). loaddata doesn't use the database structure (with intermediate the table and all), but the checks the actual fields of the model.
To avoid ending up with double entries, you will also have to remove all the json entries in the items.json file that are pointed to by the original mysql database in the myapp_book table.
Another solution would be to use http://pypi.python.org/pypi/py-mysql2pgsql (see also this question)
Upvotes: 1
Reputation: 599630
Seems like for whatever reason the Books schema in your postgres db doesn't match the models - it has a location_id column. You should drop the table and rerun syncdb.
Upvotes: 0