1

モデルを作成しましたが、'makemigrations' と 'syncdb' の後、テーブルがデータベースに表示されません。これは私が作成したい 2 番目のモデルで、最初のモデルは問題ありませんでした。コマンド「makemigrations」は .py ファイルを作成しますが、syncdb は「No migrations 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
4

2 に答える 2

2

Django 1.7 を使用している場合は、使用しているためmakemigrationssyncdb推奨になっていると思います...使用する必要がありますmigrate

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

参照:
https://docs.djangoproject.com/en/1.7/releases/1.7/#schema-migrations

于 2014-11-28T12:11:26.043 に答える
0

新しいデータベースで新しいアプリを作成するときは、次の順序でコマンドを実行します。

1) python manage.py syncdb

これにより、django (例: :auth_group、auth_user) などで使用されるすべてのテーブルが作成されます。このコマンドの後、データベースにアプリのテーブルは表示されません。

次に、次のようにします。

2) python manage.py makemigrations

これにより、初期移行が実行され、すべての新しいモデルが検出されます。モデルが検出されるだけです。データベースにテーブルは作成されません。

その後、次のコマンドを実行します

3) python manage.py 移行

これにより、テーブルが作成されます。

これを行った後は、(2) と (3) を使用して変更を行うだけです。変更には、新しいモデルの作成や既存のモデルのスキーマの編集などがあります。

だから今、あなたの質問で、あなたが走ったときpython manage.py makemigration、それはあなたの新しいモデル「テキスト」を検出しました

python manage.py migrate変更を有効にするために実行するだけです(この場合は、テーブルを作成するため)。

お役に立てれば

于 2014-11-28T12:36:20.623 に答える