我想从MySQL数据库导出一些数据并将其导入PostgreSQL数据库。这两种模式描述如下:为什么在尝试导入JSON时收到IntegrityError?
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)
注意,Book
模型从Item
模型继承。 (另外,我也知道author
真的应该是一个单独的模型 - 但我需要一些简单的说明问题。)我第一次尝试将数据从模型使用dumpdata
命令导出:
的JSON在items.json
看起来是这样的:
{
"fields": {
"title": "Introduction to Programming",
"location": 1
},
"model": "myapp.item",
"pk": 1
}
的JSON在books.json
看起来是这样的:
{
"fields": {
"author": "Smith, Bob"
},
"model": "myapp.book",
"pk": 1
}
我可以导入locations.json
和items.json
没有问题,但只要我尝试导入books.json
,我遇到了以下错误:
IntegrityError: Problem installing fixture 'books.json': Could not load myapp.Book(pk=1): null value in column "location_id" violates not-null constraint
编辑:为myapp.books
模式(根据PostgreSQL的本身)如下:
# SELECT * FROM myapp_book; item_ptr_id | author -------------+-------- (0 rows)
我不知道为什么这是downvoted? –