39

After some errors, I dropped my database, deleted all my migration files (I left init.py). Now, when I run

python migrate.py makemigrations   // It creates migrations correctly
python migrate.py migrate          // It outputs "app.0001_initial OK"

But absolutely NO table (related to my app) is created. Only those related to django are. And in the migration table, my application migration is marked is done but no table have been created as I said, it's very displeasing.

Here is an excerpt my migration file :

# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-02-18 21:59
from __future__ import unicode_literals

import colorful.fields
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Client',
            fields=[
                ('id', models.AutoField(db_column='idtblclients', primary_key=True, serialize=False)),
                ('genre1', models.CharField(blank=True, max_length=10)),
            ('prenom1', models.CharField(blank=True, max_length=45)),
            ('nom1', models.CharField(blank=True, max_length=45)),
            ('genre2', models.CharField(blank=True, max_length=10)),
            ('prenom2', models.CharField(blank=True, max_length=45)),
            ('nom2', models.CharField(blank=True, max_length=45)),
            ('courriel', models.CharField(blank=True, max_length=45)),
            ('langue', models.CharField(blank=True, max_length=1)),
            ('numtel1', models.CharField(blank=True, db_column='NumTel1', max_length=20)),
            ('numtel2', models.CharField(blank=True, db_column='NumTel2', max_length=20)),
            ('numcivique', models.CharField(blank=True, db_column='NumCivique', max_length=15)),
            ('rue', models.CharField(blank=True, db_column='Rue', max_length=45)),
            ('ville', models.CharField(blank=True, db_column='Ville', max_length=45)),
            ('codepostal', models.CharField(blank=True, db_column='CodePostal', max_length=45)),
            ('timestamp', models.DateTimeField(blank=True, db_column='Timestamp', null=True)),
            ('zone', models.CharField(blank=True, db_column='Zone', max_length=45)),
        ],
        options={
            'db_table': 'tblclients',
            'managed': False,
        },
    ),
....

Do you have an idea how to fix it ?

6
  • 2
    Have you added the app in settings.py?
    – gtlambert
    Feb 18, 2016 at 22:50
  • Yes sure. When i run migrate, django tells me "app.0001_initial OK" which should mean the migration have been done (while they haven't been done as i said) Feb 18, 2016 at 22:58
  • And i forgot to tell that the migration ignores all the migrations in my app migration file (the one I have copied) but the last one which is a table that has foreign keys with the first others. So mysql outputs an error. Feb 18, 2016 at 23:00
  • 8
    try python manage.py makemigrations <appname_thats_missing_tables> and python manage.py migrate <appname_thats_missing_tables>
    – rwx
    Feb 18, 2016 at 23:50
  • Ah you're right. How didn't I see that ! Thank you. Feb 19, 2016 at 0:20

15 Answers 15

95
python manage.py migrate --fake APPNAME zero

This will make your migration to fake. Now you can run the migrate script

python manage.py migrate APPNAME

Tables will be created and you solved your problem.. Cheers!!!

5
  • 1
    Definitely works for Django 2.1.5, also if you deleted your migrations folder intentionally or by mistake Mar 28, 2019 at 9:15
  • python manage.py makemigrations <APPNAME> followed by python manage.py migrate is the steps worked for me
    – webjockey
    Apr 8, 2019 at 18:32
  • This is work for me. If some table already exists in DB, drop the all table first and run the second cmd.
    – lwin
    Jul 3, 2019 at 8:14
  • This was great. If you need your database to be reset, this works fantastically.
    – Chris Fowl
    Feb 27, 2020 at 10:13
  • 1
    Wow, what just happened? It somehow worked! What do the above lines do? how it work? Anyone?
    – Arpan
    Nov 4, 2020 at 13:58
34

From Django docs, Options.managed: "If False, no database table creation or deletion operations will be performed for this model."

And I see you have

   options={
        'db_table': 'tblclients',
        'managed': False,
    },

Try setting managed=True in the model.

0
21

In my case, what created the tables was this:

python manage.py migrate --run-syncdb

I am using Django 1.9.6.

3
  • This solved the problem for me after I dropped my database and deleted all migrations Jul 8, 2019 at 8:21
  • Thank you, this was exactly what I needed, followed by a fake-initial Nov 7, 2019 at 21:05
  • 1
    I deleted all migrations - all of them. Then this worked, i.e. table was created. But it created no migrations. How is this possible?
    – Gathide
    Jun 8, 2020 at 17:35
17

I had manually deleted one of the tables and the trick that worked for me was to execute the following steps:

  1. Removed the migrations folder.
  2. deleted the migrations from db: DELETE from django_migrations WHERE app='alerts_db';
  3. python manage.py migrate --run-syncdb' before
  4. python manage.py makemigrations 'app_name'
  5. python manage.py migrate --fake

Note: earlier I was not executing the #3 step and the table was not getting created. my django version: v3.0.5

2
  • 1
    it's 2021 and django3, and this DELETE from django_migrations WHERE app='alerts_db'; command helped me.
    – burney
    Feb 15, 2021 at 9:24
  • Amazing! It's the right way to solve the problem! Thanks.
    – chilun
    Apr 21, 2021 at 14:24
9

I face same issue:

python manage.py migrate poll
Operations to perform:
  Apply all migrations: poll
Running migrations:
  No migrations to apply.

Follow these steps to create tables for your app.

Go to your app folder

1.delete migration folder.

2.python manage.py makemigrations.

3 Rename 0001_initial.py file to 0001_initial_manual.py.

4 python manage.py migrate APPNAME.

After this my tables created smoothly.

python manage.py migrate poll
Operations to perform:
  Apply all migrations: poll
Running migrations:
  Applying poll.0002_initial... OK
1
  • before step 4, i need to drop all created tables <app_name>_<model_name> and constraints that were generated by Django.
    – BingLi224
    May 28, 2019 at 13:53
4

This worked in Django v.3 :

  1. Delete the rows related to your application from database:

    DELETE FROM django_migrations WHERE app='<app_name>';
    
  2. Delete your app's migration folder

  3. Finally

    ./manage.py makemigrations <app_name>
    
    ./manage.py migrate <app_name>
    
  4. Done. Table created.

0
1

Please check if you are using multiple databases in your project. If multiple databases configured, check the default database is the one you are looking for.

If default and your expected database are different, run below command pointing to your db configuration.

python manage.py migrate $app_name --database $dbname_from_setting

Hope this helps.

1

The answer to this depends if you already have a history of migrations in your app folder. In my case I didn't...I think I deleted original migration scripts.

So following the Django docs, I did the following:

  1. Comment out new updates in models.py
  2. Follow section Adding migrations to apps...
  3. Create migrations for what is already existing:

    python manage.py makemigrations yourapp

  4. Do fake applying of these migrations (below singles that the tables already exist and not to try to recreate):

    python manage.py migrate --fake-initial

  5. Uncomment changes in model.py
  6. Follow steps in section Workflow ...

    python manage.py makemigrations yourapp

    python manage.py migrate

1

First try this;

Check your migration files for the app, if you have the create model portion in your migrations file, remove it and redo migrations. I.e remove this; migrations.CreateModel( name='YourModelName', ....... .....)

(venv)root@debian:~/Desktop/project$ python manage.py makemigrations appName
(venv)root@debian:~/Desktop/project$ python manage.py migrate appName

Else; navigate to your database and delete all migrations for that app, you may have to drop the related tables too.

postgres=# \c db_yourdb
db_yourdb=# delete from django_migrations where app='appName';

then go to your code and;

(venv)root@debian:~/Desktop/project$ python manage.py makemigrations appName
(venv)root@debian:~/Desktop/project$ python manage.py migrate appName

To do fresh migrations entirely, first do the necessary deletions for migrations and droping the tables if need be then;

(venv)root@debian:~/Desktop/project$ find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
(venv)root@debian:~/Desktop/project$ find . -path "*/migrations/*.pyc"  -delete

finally do;

(venv)root@debian:~/Desktop/project$ python manage.py makemigrations
(venv)root@debian:~/Desktop/project$ python manage.py migrate

Read more here on how to reset migrations

0

This is how I got @JulienD's answer to work:

  1. I added metadata to my model def in models.py:

class thing(models.Model): name = models.CharField(max_length=200) class Meta: app_label = 'things' managed = True

  1. Execute:

python manage.py makemigrations

  1. Execute:

python manage.py migrate

After that python manage.py showmigrations shows the migrations and the admin site started working.

0

Delete folder migrations and re-run. It works for me

1
  • 1
    You should NOT delete the migrations folder, but you can delete its content, but it's VERY important to keep the init.py in it. Jul 9, 2019 at 13:58
0

I had also that problem, all I had to do to solve it was to(I was using phpymyadmin):

1. Go into phpmyadmin panel

Pick the database I connected to, and empty the table named django_migrations

Then, I did the usual: python manage.py makemigrations app_name

Lastly: python manage.py migrate app_name

And voi'le, done and ready ;)

0

Add your app name in settings.py as per the following: https://docs.djangoproject.com/en/3.2/ref/applications/

0

I recently faced this issue and I identified the problem was that my app, (which was also added in settings), started with an uppercase letter. It is interesting that no errors were ever thrown to warn about this, but I kept seeings errors that certain tables related to the app do not exist.

So, make sure your app name starts with a small letter, and python manage.py makemigrations <appName> and python manage.py migrate

0

I had a similar problem too.

py manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions, token_blacklist Running migrations: No migrations to apply.

After checking lots of solutions finally, I found that I've used the same environment for many projects. I was running manage.py of another project. The case might be the same as mine. If the case is not as similar to mine then all the solutions of the above works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.