Django - 将模型迁移到MySQL数据库

时间:2016-11-04 00:54:01

标签: python mysql django

ProgrammingError at /blog/
(1146, "Table 'project.blog_category' doesn't exist")

不确定导致此问题的原因。迁移数据似乎不足以填充MySQL数据库。我也尝试过syndb。

root@watchtower:/project# ./manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: messages, staticfiles, watchtower
  Apply all migrations: blog, contenttypes, sessions, admin, auth
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.

我可以手动创建数据库表吗?这是model.py

from django.db import models
from django.db.models import permalink

class Blog(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateTimeField(db_index=True, auto_now_add=True)
    category = models.ForeignKey('blog.Category')

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_post', None, { 'slug': self.slug })

class Category(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_category', None, { 'slug': self.slug })

2 个答案:

答案 0 :(得分:0)

from django.db import models
from django.db.models import permalink

class Blog(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateTimeField(db_index=True, auto_now_add=True)
    category = models.ForeignKey('Category')

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_post', None, { 'slug': self.slug })

class Category(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_category', None, { 'slug': self.slug })

我将您的外键关系从blog.Catergory更改为Category

此外,我建议删除app文件夹中migrations文件夹下的迁移文件。删除除__init__.py之外的所有文件。除此之外,您还可以在MySQL中删除数据库,然后分别应用python manage.py makemigrationspython manage.py migrate(请记住在应用迁移之前创建它)。

答案 1 :(得分:0)

在运行之前,您需要运行manage.py makemigrations来创建迁移文件。