python manage.py syncdb不会创建表

时间:2014-11-28 12:01:56

标签: python django models syncdb

我创建了一个模型但是在制作之后#m;和' syncdb'该表不会出现在数据库中。 这是我想要创建的第二个模型,第一个模型不是问题。 命令' makemigrations'创建.py文件,但syncdb显示消息"没有要应用的迁移"。

我做错了什么?

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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时,它检测到您的新模型&#34;文字&#34;

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

希望这有帮助