Sahand
Sahand

Reputation: 8370

Tables not showing up and migrations not visible after `python manage.py migrate`

I'm following the official tutorial on Django and have run python manage.py migrate in https://docs.djangoproject.com/en/1.11/intro/tutorial02/ . After this, I'd expect some files to show up in polls/migrations, but there is just an empty __init__.py there, and when I run sqlite3 and type .tables or .schema, nothing is output. Still, the python manage.py migrate command seems successful:

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

What's going wrong here?

EDIT:

Added 'polls', to my INSTALLED_APPS. Then:

$ python manage.py makemigrations
Migrations for 'polls':
  polls/migrations/0001_initial.py
    - Create model Choice
    - Create model Question
    - Add field question to choice
(django) Sahands-MacBook-Pro:mysite sahandzarrinkoub$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying polls.0001_initial... OK
(django) Sahands-MacBook-Pro:mysite sahandzarrinkoub$ sqlite3
SQLite version 3.16.0 2016-11-04 19:09:39
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .schema
sqlite> 

Same problem.

EDIT2: Upon running python manage.py dbshell, .schema and .tables finally produced output.

Upvotes: 1

Views: 4336

Answers (3)

Dheeraj Kumar
Dheeraj Kumar

Reputation: 1

Create 'migrations' directory inside your app and create and empty file with name "init.py" in it. Then run makemigrations and migrate commands.

This solved my problems.

Upvotes: 0

wencakisa
wencakisa

Reputation: 5958

At first, check if your polls app exists in your INSTALLED_APPS list in settings.py:

INSTALLED_APPS = [
    # ...
    'polls',
]

And then try to run makemigrations before doing migrate:

$ python manage.py makemigrations
$ python3 manage.py migrate

EDIT:

Now, as your polls table is created in your database, you can access the sqlite3 client by running $ python manage.py dbshell:

$ python manage.py dbshell
sqlite> .schema

The difference between running just sqlite3 and python manage.py dbshell is said in the docs:

django-admin dbshell

Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings.

Upvotes: 3

Swapnil
Swapnil

Reputation: 2603

You might have done something wrong or you might not have. It depends

  1. If you have not created any model in models.py file and you have registered you app in settings.py INSTALLED_APPS

In this case, you are not doing anything wrong. Django will create a migration in the migrations folder of your app when you have created at least one model and you have register your app in INSTALLED_APPS

  1. If you have created models but have forgotten to add your app to INSTALLED_APPS

In this case, you just have to add your app's name to INSTALLED_APPS

Hope this helps

Upvotes: 0

Related Questions