[Django]-Django migrate : doesn't create tables

35👍

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.

100👍

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!!!

22👍

In my case, what created the tables was this:

python manage.py migrate --run-syncdb

I am using Django 1.9.6.

18👍

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

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

5👍

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.

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.

👤Durai

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

👤7guyo

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

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

👤Denmau

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.

Leave a comment