2

pretty new to Django, i'm encountering an issue with a new model (and a new app 'blog' i made). The table blog_post didn't exist after configuring the model and makemigration.

Here is the all process i did. I'm following official tutorial:

Here is my blog/models.py:

from django.db import models

    class Post(models.Model):
        title = models.CharField(max_length=80)
        text = models.TextField()
        author = models.ForeignKey('auth.User', on_delete= models.CASCADE)
        created_date = models.DateTimeField()
        pub_date = models.DateTimeField()

    def publish():
        self.pub_date = timezone.now()
        self.save()

mysite/settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog.apps.BlogConfig',
]

After the first

python manage.py makemigrations blog

Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Post

python manage.py sqlmigrate blog 0001

BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,     "title" varchar(80) NOT NULL, "text" text NOT NULL, "created_date" datetime NOT NULL, "pub_date" datetime NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
COMMIT;

python manage.py migrate

Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
No migrations to apply.

So, here it is. The new table didn't seem to be created. I check with a SqLite utility and the is no such table: blog_post I also check with django shell.

I double (triple) check the process:

  1. Change your models (in models.py).
  2. Run python manage.py makemigrations to create migrations for those changes
  3. Run python manage.py migrate to apply those changes to the database

But i'm stuck at this point. Can someone tell me what i missed ? Thank you !

Here is my database settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

python manage.py showmigrations

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
blog
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial

I check if table exist with DB Browser for SQLite, but blog_post don't exist.

Link to the github repo: https://github.com/mothinx/juliengracia

8
  • have you tried running python manage.py makemigrations without the blog inside the command? Commented Feb 20, 2018 at 11:14
  • No changes detected is the result. Commented Feb 20, 2018 at 11:16
  • 1
    The migration is created but not applied. python manage.py migrate blog should do it. Commented Feb 20, 2018 at 11:17
  • 1
    Are you sure the table doesn't exist? How are you checking? What are your database settings? Where are you running this code? Commented Feb 20, 2018 at 11:20
  • 1
    What's the result of manage.py showmigrations ? Commented Feb 20, 2018 at 11:45

3 Answers 3

6

The output of showmigrations shows that Django thinks the initial blog migration ran.

blog
 [X] 0001_initial

The sqlmigrate output shows that the migration should have created the table. Therefore it looks as if the django_migrations folder and the database are out of sync.

You could try re-running the initial migration by faking back to the zero migration.

python manage.py migrate --fake blog zero
python manage.py migrate blog

If that doesn't work, and you don't have any important data in the database, you could delete the db.sqlite3 file and run python manage.py migrate again.

Sign up to request clarification or add additional context in comments.

2 Comments

python manage.py migrate --fake blog zero did the trick.For my culture, this option --fake crush the fact that django though table was created ? Thanks a lot man !
Glad that worked. I'm not sure I understand your follow up question. If you did python manage.py migrate blog zero (without --fake), then Django would try to reverse the migration by dropping the tables. That would fail because the tables didn't exist. You use --fake so that Django updates the django_migrations table without trying to update the database. Then when you run python manage.py migrate blog, it will run the 0001_initial migration and create the table.
0
python manage.py migrate --fake APPNAME zero

And then you can run the migrate script

python manage.py migrate 

Hope this helps!

Comments

-1

I just use the following:

python manage.py makemigrations
python manage.py migrate

2 Comments

If the problem was with indentation none (or almost none) of the manage.py command would work, and specially not makemigrations.
Then I guess it was problem with copy pasting the code. My bad. I will delete my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.