Search code examples
djangodjango-modelsdatabase-migration

Django database migration not working, new table not created


I initially created a database in mysql and used inspectdb to create all the models. So far so good, but then I started trying to make changes to the models and migrating them. I added columns to no avail, there were no changes in the database even though the migrations folder showed the changes. I began to wonder if it was something to do with case, so I tried again with a second model TestTwoT with table name and all fields in lower case. I then ran the make migrations which produced this output:

Migrations for 'testdb':
  testdb/migrations/0010_testtwot.py
    - Create model TestTwoT

The generated .py file in the migrations folder (which looks fine to me):

# Generated by Django 3.0.1 on 2020-02-10 22:46

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('testdb', '0009_auto_20200210_2241'),
    ]

    operations = [
        migrations.CreateModel(
            name='TestTwoT',
            fields=[
                ('id', models.AutoField(db_column='id', primary_key=True, serialize=False)),
                ('name', models.CharField(blank=True, db_column='name', max_length=1000, null=True)),
            ],
            options={
                'db_table': 'test_two_t',
                'managed': True,
            },
        ),
    ]

I then ran the migrate script which produced this output suggesting there were no problems creating the table:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, testdb, sessions
Running migrations:
  Applying testdb.0010_testtwot... OK

However, still no sign of my new table when I run the 'show tables' sql command or the 'describe test_two_t' command.

There is another complication in my scenario in that I am not using the default database, but my setup seems entirely OK and my application is working fine pointing to the database.

EDIT : Following Borut's advice I changed the commands to (adding the --database parameter):

python3 manage.py makemigrations
python3 manage.py migrate --database=testdb

EDIT2 : It's all working perfectly now (after I also deleted all the previous migration .py files and started from scratch with the corrected above commands).


Solution

  • Following Borut's advice I changed the commands to (adding the --database parameter):

    python3 manage.py makemigrations
    python3 manage.py migrate --database=testdb
    

    It's all working perfectly now (after I also deleted all the previous migration .py files and started from scratch with the corrected above commands).