将django sqlite数据库迁移到postgresql

时间:2016-05-10 00:34:43

标签: python django database postgresql sqlite

自从我投入生产以来,我已经尝试将sqlite数据库迁移到更稳定的postgresql很长一段时间了。我遇到了一些问题,但是我遇到了障碍而又无法进一步解决问题。

要获得备份,我使用 settings.py 中配置的sqlite运行./manage.py dumpdata --exclude auth.permission --exclude contenttypes --natural-foreign > db.json

配置postgresql数据库后,我运行./manage.py migrate并在 settings.py 中配置了postgresql。

最后我跑了./manage.py loaddata db.json并收到以下错误:

django.db.utils.ProgrammingError: Problem installing fixture '/home/ubuntu/bl/loom/db.json': Could not load web.Project(pk=18): operator does not exist: character varying = integer
LINE 1: ...INNER JOIN "web_project_tags" ON ("web_tag"."tag" = "web_pro...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

错误所引用的模型具有以下代码:

class Project(models.Model):
    owner = models.ForeignKey(User, related_name='owner')
    name = models.CharField(max_length=50)
    img = models.ImageField("Cover", upload_to="img/projects", blank=True, null=True)
    vid = models.URLField("Youtube Link", null=True, blank=True)
    desc = models.TextField("Description", max_length=500)
    stakeholders = models.ManyToManyField(Profile, related_name='stakeholders', blank=True)
    industry = models.ManyToManyField(Industry, related_name="industry")
    tags = models.ManyToManyField(Tag, related_name="project_tag")
    is_private = models.BooleanField("Is this a private project?", default=False, help_text="Private projects are not shown on the index.")
    b_type = models.ForeignKey(B_type, help_text="What type of project is this")
    role = models.ForeignKey(Role, related_name="role")
    deleted = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = "Project"
        verbose_name_plural = "Projects"

class Tag(models.Model):
    tag = models.CharField("Tag", max_length=100, primary_key =True)

    def __unicode__(self):
        return self.tag

    class Meta:
        verbose_name = "Tag"
        verbose_name_plural = "Tags"

更新: 日志文件包含更详细的错误,它显示完整的查询。

2016-05-09 00:54:39 UTC ERROR: operator does not exist: 
character varying = integer at character 89 
2016-05-09 00:54:39 UTC HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
2016-05-09 00:54:39 UTC STATEMENT: SELECT "web_tag"."tag" FROM "web_tag" INNER JOIN "web_project_tags" ON ("web_tag"."tag" = "web_project_tags"."tag_id") WHERE "web_project_tags"."project_id" = 18

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题,希望这个答案会对某人有所帮助。

检查日志后,我检查了postgresql数据库中是否有错误。事实证明,即使迁移完成了最新版本,web_project_tags的数据类型也是错误的。我使用以下方法更正了此数据类型:

ALTER TABLE problematic_table ALTER COLUMN problematic_column TYPE character varying(100);

有了这个,我最终用./manage.py loaddata db.json加载了数据而没有错误。