python manage.py syncdb doesn't create a table

4.5k views Asked by At

I created a model but after 'makemigrations' and 'syncdb' the table doesn't appear in the database. This is the second model that I want to create and the first model was not a problem. The command 'makemigrations' creates a .py file, but syncdb shows the message "No migrations to apply".

What am I doing wrong?

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

The error code

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

There are 2 answers

5
Ymartin On

When you make a fresh app with a fresh database, you run the commands in following order:

1) python manage.py syncdb

This creates all the tables that will be used by django (eg :auth_group, auth_user) etc. You won't see your app's tables, after this command, in the database.

Then you do:

2) python manage.py makemigrations

This will run the initial migration and will detect all your new models.It just detects the models.Doesn't create tables in the database

After that,run the following command

3) python manage.py migrate

This will create your tables.

After you have done this, you just need to use (2) and (3) for any changes to happen.The changes could be creating new models or editing the schema of existing models, etc.

So now, in your question, when you ran python manage.py makemigration ,it detected your new model "text"

Now just run python manage.py migrate for the changes to take effect(in this case, to create table).

Hope this helps

0
Anentropic On

if you're using Django 1.7, which I assume since you're using makemigrations then syncdb is deprecated... you should be using migrate

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

see also:
https://docs.djangoproject.com/en/1.7/releases/1.7/#schema-migrations