1

First, the existing model is in '0001_initial.py'.

After adding the 'models' I added, and when I did 'makemigrations', It's going into '0002_xxx.py'.

I succeeded in Migrate, and there was no problem with the database.

And then when these 'models' were added, I wanted to put the initial data into the database.

so I made fixture folder for putting in initial data after make migrations.

I put the json file in it.

However, when I changed the name of 'model' and did 'makemigrations' again, I had an error finding the table.

So I went directly to sqlite and erased all the tables on the new models.

Then I did makemigrations.

After that,

python3 manage.py migrate sbimage

When I do 'migrate' here like this,

django.db.utils.OperationalError: table "sbimage_camerathreshold" already exists

There's an error like this.

python3 manage.py migrate sbimage --fake

This has made both 0001, 0002 'FAKED'.

It's my 0002 file was created after 'makemigrations'.

from django.db import migrations, models

def load_my_initial_data(apps, schema_editor):
    call_command("loaddata", "addition.json")

class Migration(migrations.Migration):

    dependencies = [
        ('sbimage', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='AuthNumberR',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('auth_number_r', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='AuthNumberT',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('auth_number_t', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='Claimant',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('claimant', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='CountryOfOrigin',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('country_of_origin', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='EquipmentName',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('equipment_name', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='FccId',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('fcc_id', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='Manufacturer',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('manufacturer', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='NbId',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('nb_id', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='Publisher',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('publisher', models.CharField(max_length=64)),
            ],
        ),
        migrations.CreateModel(
            name='WarningStateList',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('warning', models.CharField(max_length=4096)),
            ],
        ),
        migrations.CreateModel(
            name='WifiWarningStateList',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('wifi_warning', models.CharField(max_length=4096)),
            ],
        ),
        migrations.RunPython(load_my_initial_data),
    ]

How do I create a table of newly created models in this situation?

  • This code dosen't work at all.

    python3 manage.py migrate sbimage 0002

    Running migrations: No migrations to apply.

2 Answers2

0

Ideally, you should never delete the tables from database directly.

You can revert the database using migration command.

./manage.py migrate my_app 00xx_migration_file_you_want_to_revert_to.py

For now, delete all the migration files and rerun the makemigration and migrate command.

sp1rs
  • 786
  • 8
  • 23
  • 1
    just a note: You don't need to enter the full name, u just need the number `00xx`. Also, you can use `migrate app zero` to un-migrate the whole app – rabbit.aaron Jul 05 '19 at 04:54
0

You should NEVER try and manipulate your database directly, while you are using a code first approach for your database. As far as your problem is concerned your migration is not applying because migrations always apply in a sequence. Suppose you have 6 migrations that you have already migrated into, then you create a 7th migration, that migration will check for whether or not your previous migrations are in sync with your database. In your case, you hard coding the database directly has caused a discrepancy in your model. The most hassle free approach would involve you

  1. Deleting your migrations folder from your django application.
  2. Delete all the data from your migrations table in your database.
  3. python manage.py makemigrations
  4. python manage.py migrate --fake

After that you can reapply your migrations to sync your model to the database.

Moeed
  • 3
  • 6
  • At no.2 should I drop the django_migrations table? or DELETE FROM django_migrations WHERE app = 'sbimage'; with this code will be fine? – Junhyun Kyung Jul 05 '19 at 06:24
  • @JunhyunKyung DELETE FROM django_migrations WHERE app = 'sbimage'; works perfectly fine. – Moeed Jul 05 '19 at 06:26
  • I tried it in this order, but it didn't produce a table. python3 manage.py loaddata addition.json error message : no such table: sbimage_warningstatelist – Junhyun Kyung Jul 05 '19 at 06:29
  • I cant see your database so cant really get to the root cause of this problem, you need to make sure all your models/fields in the codes have the same properties/names as the tables/fields in the database. Also as an alternative to step 4 you can run python manage.py migrate --run-syncdb – Moeed Jul 05 '19 at 06:40
  • 1
    python3 manage.py migrate sbimage --run-syncdb CommandError: Can't use run_syncdb with app 'sbimage' as it has migrations. Migrate is not working. – Junhyun Kyung Jul 05 '19 at 06:52
  • python3 manage.py migrate sbimage --fake Operations to perform: Apply all migrations: sbimage Running migrations: Applying sbimage.0001_initial... FAKED – Junhyun Kyung Jul 05 '19 at 06:53
  • .tables auth_group django_content_type auth_group_permissions django_migrations auth_permission django_session auth_user sbimage_camera auth_user_groups sbimage_camerathreshold auth_user_user_permissions sbimage_label django_admin_log sbimage_maker these are my current tables. – Junhyun Kyung Jul 05 '19 at 06:54
  • Refer to this answer, probably would be of more help to you. [link](https://stackoverflow.com/a/29253574/11269948) – Moeed Jul 05 '19 at 07:04