Django migrate : doesn't create tables

Question:

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 ?

Asked By: Adam Cherti

||

Answers:

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.

Answered By: JulienD
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!!!

Answered By: Vignesh

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
Answered By: Rajiv Sharma

In my case, what created the tables was this:

python manage.py migrate --run-syncdb

I am using Django 1.9.6.

Answered By: Roddy P. Carbonell

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.

Answered By: Geordie

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.

Answered By: Durai

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

Answered By: Doug Nintzel

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

Answered By: ben othman zied

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

Answered By: 7guyo

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

Answered By: ankit tyagi

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 😉

Answered By: Szymon D.

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.

Answered By: smrachi

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

Answered By: Fisal Assubaieye

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

Answered By: Denmau

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.

Answered By: Vkash Poudel
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.