[Solved] Django 1.8 migrate is not creating tables

yekabathula-macbookair2:roster yekabathula$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, api, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying api.0001_initial... 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 sessions.0001_initial... OK
yekabathula-macbookair2:roster yekabathula$ python manage.py syncdb
/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, api, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.

After doing python manage.py migrate, tables are not created in database from my models.py it is able to create other tables from django_session etc. Is there anything else that I need to follow here ?

Enquirer: YSK

||

Solution #1:

I was facing a similar problem in Django 1.10 and none of the above solutions worked for me.

What eventually worked was running this command:

python manage.py migrate --fake myappname zero

This reset all migrations (to the zeroth state)

This followed by :

python manage.py migrate myappname

created the tables for me.

If you do not want to roll back to the initial(zero) state but say to the migration number 0005(the last migration that worked), you can instead do this:

python manage.py migrate --fake myappname 0005

And then proceed with the actual migrate:

python manage.py migrate myappname

More details in the docs

Respondent: bhaskarc

Solution #2:

In my case the __init__.py file was missing from the APP/migrations/ folder. If you don’t have one, all it needs is an empty __init__.py file.

Respondent: Ljubitel

Solution #3:

I ran into the same problem. After lots of digging, I found the solution.
I’m using django 1.11.

If you want to start-over,

1)delete all the files in your migrations folder except __init__.py
2)drop database
3)create database
4)python makemigrations
5)python migrate

if you have reset_db, instead of 2nd and 3rd steps you can use reset_db.

python manage.py reset_db
Respondent: Vkreddy

Solution #4:

I had a similar problem and just figured it out.I have multiple databases. My local one (the one not being updated) is a MySQL database. The others are MS SQL Server and MySQL. I have routers to the other databases since I do not manage them and had (in Django 1.6) used the routers to indicate allow_sync() = False. With 1.7, I changed that to allow_migrate() = False. BUT I DID NOT ADD A ROUTER FOR MY LOCAL DATABASE. The default appears to be allow_migrate() = False if there is not one. As a result, the migrations just failed silently ( Reference: https://docs.djangoproject.com/en/1.7/topics/db/multi-db/). I added a router for my local DB, setting allow_migrate() to return True and now my migrations actually create my tables.

Respondent: user3130740

Solution #5:

I am using MySQL and get into this issue after deleting 0001_initial.py migration file and all the custom tables evolved from DB to try to regenerate all they…

Solved this issue simply deleting these rows in django_migrations table…

enter image description here

After that, $ python manage.py migrate command regenerates all my custom tables again.

Respondent: JavierFuentes

Solution #6:

Change managed = True (if it is set to False) in models.py

class Meta:
    managed = False
    db_table = 'table_name'

To

class Meta:
    managed = True
    db_table = 'table_name'
Respondent: Jithin

Solution #7:

This solved the problem for me (I am using MySQL workbench by the way):

  • Run this sql: SET FOREIGN_KEY_CHECKS = 0;
  • Select all the tables in your django database (click on the first table, then press and hold shift, then click on the last table). Then right click and choose “Drop n tables” (where n is the number of tables you just selected)
  • then run python manage.py migrate
  • Finally restore foreign key check settings by running this sql: SET FOREIGN_KEY_CHECKS = 1;

Note: Before taking this drastic measure, I tried what Paulo Pessoa said in his comment, but still I got “No migrations to apply.” messages. However, this solved the issue.

Respondent: S?????ƒ??

Solution #8:

This can be very frustrating, but here is method that worked for me.
First, if you’ve deleted the migration file go ahead and create it again

python manage.py makemigrations

Then compare the migration file with the SQL raw code and if it fit then apply migration

python manage.py sqlmigrate [app_name] 0001
python manage.py migrate

or you can simply pipe it from your command line or terminal

python manage.py sqlmigrate [app_name] 0001 | [psql] db_name username

BEGIN
CREATE TABLE
ALTER TABLE
COMMIT

NOTE: In my case I’m using postgresql. Though engine is similar to other database langauge, you might not get expected result.

Respondent: Romeo

Solution #9:

I had a similar issue. I did everything stated above but nothing worked. Then I realized I had not added my model name in admin.py file in my app.

Along with everything stated above you have to also add your model name in admin.py file. You have to add it like this:

admin.site.register(model name)
Respondent: Parag Ingale

Solution #10:

  • Delete existing tables of the models in MySQL database.

  • Delete migration folder under app folder.

  • Delete all relative migration records in the table
    “django_migrations” from MySQL.

  • Now you get clear model and database. Use
    python manage.py makemigrations and python manage.py migrate
    to create tables.

Hope to help you.

Respondent: Robin Wu Yu

Solution #11:

try this one,

run,

python manage.py makemigrations app_name 

above command will make migrations, if successful then run second command or
check if you have typo in Installed_app and also check for AppConfig module

python manage.py migrate app_name

if above was successful, this will create the table in db

Respondent: mananbh9

Solution #12:

Problem: : When you apply migrations in django for the first time, django creates table of that model in database and marks somewhere in its own file(class):

`initial = True` 
  • When you then tries to alter the schema of that table it firstly checks
    if initial = True

  • if an initial class attribute isn’t found, a migration will be considered “initial”

  • In case the initial = True we need to use 

    python manage.py migrate --fake-initial
    

For an initial migration Django checks that all of those tables already exist in the database and fake-applies the migration if so. Similarly, for an initial migration that adds one or more fields Django checks that all of the respective columns already exist in the database and fake-applies the migration if so.

Fake initial migration uses both CreateModel() and AddField() methods.


Solution:

>> python manage.py makemigrations <AppName>
>> python manage.py migrate --fake-initial
Respondent: Zahid Khan

Solution #13:

To avoid deleting critical databases, you may also consider silencing properties below the Class Meta: for example this model:

class Blog(models.Model):
    category = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    date_added = models.DateTimeField()
    merits = models.CharField(max_length=300, blank=True)
    demerits = models.CharField(max_length=300, blank=True)
    class Meta:
        managed = True
        db_table = 'weblog'
        verbose_name_plural = "Blog"

    @property
    def content_min(self):
        return truncatechars(self.content, 50)

You can then run makemigrations and migrate and your table will be created.

Respondent: Kipngetich Yegon

Solution #14:

  1. If you want to do it only for your application not from all the apps then Clear your application migrations records from ‘django_migrations’ table in your DB.
  2. Python manage.py makemigrtions
  3. python
Respondent: sunil ms

Solution #15:

  1. Delete database
  2. delete migration folder
  3. run migrate command
  4. run makemigrations command
  5. run migrate command

It will create all tables perfectly

Solution #16:

  1. Change from sqlite3 to mysql in settings.py
  2. Make sure you have correct information regarding database name and username-password
  3. Delete existing migration
  4. Make migration
  5. And then migrate
Respondent: Kathan Trivedi

Solution #17:

Make sure each app’s migrations folder has a __init__.py file.

Respondent: Devesh Pradhan

Solution #18:

I had a similar issue, the connection with DB was set correctly as all the django admin tables were created on DB side. However, no models appeared in models.py

What worked for me is running in the console:

python manage.py inspectdb > models.py

which wrote everything into a new models.py file which then I replaced with the one I had in the app folder. Then I could alter the
managed = False
into
managed = True

here the link to the documentation: https://docs.djangoproject.com/en/3.1/howto/legacy-databases/

Respondent: goldforthebold

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.