python manage.py syncdb不会创建表

问题描述 投票:1回答:2

我创建了一个模型,但在'makemigrations'和'syncdb'之后,该表没有出现在数据库中。这是我想要创建的第二个模型,第一个模型不是问题。命令'makemigrations'创建.py文件,但syncdb显示消息“No migration to apply”。

我究竟做错了什么?

python manage.py makemigrations
Migrations for 'category':
   0006_text.py:
      - Create model text
root@BB:~/Documenten/BB$ python manage.py syncdb
Operations to perform:
   Synchronize unmigrated apps: admin-tools, theming
   Apply all migrations: admin, category, contenttypes, auth, sessions
Synchronizing apps without migrations:
   Creating tables...
   Installing custom SQL...
   Installing indexes...
Running migrations:
  No migrations to apply.

My models
from django.db import models

# Create your models here.

class textitem(models.Model):
    bb_txt_lang = models.CharField(max_length=3)
    bb_txt_cat = models.CharField(max_length=30)
    bb_txt_title = models.CharField(max_length=50)
    bb_txt_body = models.TextField()
    bb_txt_footer = models.TextField()
#    bb_txt_date_from = models.DateField()
#    bb_txt_date_until= models.DateField()
    bb_txt_status = models.BooleanField(default = False)

    def __str__(self):
        return self.bb_txt_title

class forum(models.Model):
    bb_for_title = models.CharField(max_length=50)
    bb_for_body = models.TextField()
    bb_for_footer = models.TextField()
#    bb_for_date_from = models.DateField()
#    bb_for_date_until= models.DateField()

    def __str__(self):
        return self.bb_for_title

class category(models.Model):
    bb_cat_lang = models.CharField(max_length=3)
    bb_cat_desc = models.CharField(max_length=50)
#    bb_cat_image = models.ImageField()
    bb_cat_text = models.TextField()
    bb_cat_prod = models.BooleanField(default = False)
    bb_cat_sub = models.ForeignKey('category', null=True, blank=True )

    def __str__(self):
        return self.bb_cat_desc

错误代码

biidbox@BiidBox:~/Documenten/BiidBox$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: admin_tools, theming
  Apply all migrations: admin, category, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying category.0002_auto_20141128_1502...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 439, in remove_field
    self.execute(sql)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 99, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "category_forum" does not exist
python django models syncdb
2个回答
2
投票

如果你使用Django 1.7,我假设你使用makemigrations然后syncdb被弃用...你应该使用migrate

https://docs.djangoproject.com/en/1.7/topics/migrations/#the-commands

也可以看看: https://docs.djangoproject.com/en/1.7/releases/1.7/#schema-migrations


0
投票

使用新数据库创建新应用程序时,请按以下顺序运行命令:

1)python manage.py syncdb

这将创建将由django使用的所有表(例如:auth_group,auth_user)等。在此命令之后,您将无法在数据库中看到应用程序的表。

然后你做:

2)python manage.py makemigrations

这将运行初始迁移并将检测所有新模型。它只检测模型。不在数据库中创建表

之后,运行以下命令

3)python manage.py migrate

这将创建您的表。

完成此操作后,您只需使用(2)和(3)进行任何更改。更改可能是创建新模型或编辑现有模型的模式等。

所以现在,在你的问题中,当你运行python manage.py makemigration时,它检测到你的新模型“文本”

现在只需运行python manage.py migrate即可使更改生效(在本例中为创建表)。

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.