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 ?
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.
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!!!
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
In my case, what created the tables was this:
python manage.py migrate --run-syncdb
I am using Django 1.9.6.
This is how I got @JulienD’s answer to work:
- 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
- Execute:
python manage.py makemigrations
- Execute:
python manage.py migrate
After that python manage.py showmigrations
shows the migrations and the admin site started working.
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.
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:
- Comment out new updates in models.py
- Follow section Adding migrations to apps…
- Create migrations for what is already existing:
python manage.py makemigrations yourapp
- 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
- Uncomment changes in model.py
-
Follow steps in section Workflow …
python manage.py makemigrations yourapp
python manage.py migrate
Delete folder migrations and re-run. It works for me
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
I had manually deleted one of the tables and the trick that worked for me was to execute the following steps:
- Removed the migrations folder.
- deleted the migrations from db:
DELETE from django_migrations WHERE app=’alerts_db’;
- python manage.py migrate –run-syncdb’ before
- python manage.py makemigrations ‘app_name’
- 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
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 😉
This worked in Django v.3 :
-
Delete the rows related to your application from database:
DELETE FROM django_migrations WHERE app='<app_name>';
-
Delete your app’s migration folder
-
Finally
./manage.py makemigrations <app_name>
./manage.py migrate <app_name>
-
Done. Table created.
Add your app name in settings.py
as per the following:
https://docs.djangoproject.com/en/3.2/ref/applications/
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
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.
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 ?
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.
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!!!
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
In my case, what created the tables was this:
python manage.py migrate --run-syncdb
I am using Django 1.9.6.
This is how I got @JulienD’s answer to work:
- 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
- Execute:
python manage.py makemigrations
- Execute:
python manage.py migrate
After that python manage.py showmigrations
shows the migrations and the admin site started working.
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.
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:
- Comment out new updates in models.py
- Follow section Adding migrations to apps…
- Create migrations for what is already existing:
python manage.py makemigrations yourapp
- 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
- Uncomment changes in model.py
-
Follow steps in section Workflow …
python manage.py makemigrations yourapp
python manage.py migrate
Delete folder migrations and re-run. It works for me
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
I had manually deleted one of the tables and the trick that worked for me was to execute the following steps:
- Removed the migrations folder.
- deleted the migrations from db:
DELETE from django_migrations WHERE app=’alerts_db’; - python manage.py migrate –run-syncdb’ before
- python manage.py makemigrations ‘app_name’
- 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
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 😉
This worked in Django v.3 :
-
Delete the rows related to your application from database:
DELETE FROM django_migrations WHERE app='<app_name>';
-
Delete your app’s migration folder
-
Finally
./manage.py makemigrations <app_name> ./manage.py migrate <app_name>
-
Done. Table created.
Add your app name in settings.py
as per the following:
https://docs.djangoproject.com/en/3.2/ref/applications/
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
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.