1

just a question regarding migrations in Django with SQlite3:

I've created some models in my Django project following the architecture below:

    EP_project
    │   db.sqlite3
    │   manage.py
    │
    ├───ep
    │   │   admin.py
    │   │   apps.py
    │   │   models.py
    │   │   tests.py
    │   │   urls.py
    │   │   views.py
    │   │   __init__.py
    │   │
    │   ├───migrations
    │   │   │   0001_initial.py
    │   │   │   __init__.py
    │   │   │
    │   │   └───__pycache__
    │   │           0001_initial.cpython-38.pyc
    │   │           0002_address.cpython-38.pyc
    │   │           __init__.cpython-38.pyc
    │   │
    │   ├───models
    │   │   │   model_address.py
    │   │   │   model_ap.py
    │   │   │   model_as.py
    │   │   │   model_at.py
    │   │   │   model_user.py
    │   │   │   __init__.py
    │   │   │
    │   │   └───__pycache__
    │   │           model_address.cpython-38.pyc
    │   │           model_ap.cpython-38.pyc
    │   │           model_as.cpython-38.pyc
    │   │           model_at.cpython-38.pyc
    │   │           model_user.cpython-38.pyc
    │   │           __init__.cpython-38.pyc
    │   │
    │   ├───tests
    │   │       test_user.py
    │   │
    │   └───__pycache__
    │           admin.cpython-38.pyc
    │           apps.cpython-38.pyc
    │           tests.cpython-38.pyc
    │           urls.cpython-38.pyc
    │           views.cpython-38.pyc
    │           __init__.cpython-38.pyc
    │
    ├───ep_project
    │   │   asgi.py
    │   │   settings.py
    │   │   urls.py
    │   │   wsgi.py
    │   │   __init__.py
    │   │
    │   └───__pycache__
    │           settings.cpython-38.pyc
    │           urls.cpython-38.pyc
    │           wsgi.cpython-38.pyc
    │           __init__.cpython-38.pyc
    │
    └───media
        └───ad_pics
                default.jpg

The problem is that when I'm making my migrations, it seems that migrations are done properly according to the terminal messages.

And my 0001_initial.py looks like this:

from django.db import migrations, models
import django.db.models.deletion
import django_countries.fields


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='AP',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
                ('image', models.ImageField(default='default.jpg', upload_to='art_piece_pics')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ep.User')),
            ],
        ),
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('first_name', models.CharField(max_length=200)),
                ('last_name', models.CharField(max_length=200)),
                ('email', models.CharField(max_length=200, unique=True)),
                ('age', models.IntegerField()),
                ('nationality', django_countries.fields.CountryField(max_length=2)),
                ('gender', models.CharField(choices=[('M', 'Male'), ('F', 'Female')], max_length=1)),
                ('profession', models.CharField(choices=[('O', 'Owl'), ('A', 'Alf')], max_length=1)),
            ],
        ),
        migrations.CreateModel(
            name='AT',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('speciality', models.CharField(max_length=200)),
                ('ap', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ep.AP')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ep.User')),
            ],
        ),
        migrations.CreateModel(
            name='AS',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('style', models.CharField(max_length=200)),
                ('ap', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ep.AP')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ep.User')),
            ],
        ),
        migrations.CreateModel(
            name='Address',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('country', django_countries.fields.CountryField(max_length=2)),
                ('city', models.CharField(max_length=200)),
                ('street', models.CharField(max_length=200)),
                ('street_number', models.IntegerField()),
                ('zip_code', models.IntegerField()),
                ('floor', models.IntegerField()),
                ('flat', models.CharField(max_length=200)),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ep.User')),
            ],
        ),
    ]

And when I create some models in the shell it works fine:

>>> from ep.models.model_user import User
>>> from ep.models.model_ap import AP
>>> user1 = User.objects.first()
>>> user1
<User: Maxence kjhug>
>>> ap1 = AP(user=user1, name="kijhugj", image='default.jpg')
>>> ap1.user
<User: Maxence kjhug>

But once I try to save some models it tells me that some tables are none existent. Here are some of the error messages:

django.db.utils.OperationalError: no such table: ep_ap

I've looked here but can't make it work with my project, tells me it's fine:

Operations to perform:
  Synchronize unmigrated apps: django_countries, messages, staticfiles
  Apply all migrations: admin, auth, contenttypes, ep, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  No migrations to apply.

I've also looked at this here but nothing as well.

What am I missing or doing wrong? Could anyone help me out? Thanks.

UPDATED:

I did use the three migrations commands:

python manage.py makemigrations ep
python manage.py sqlmigrate ep 0001
python manage.py migrate

UPDATED 2:

And here is my INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    'ep.apps.EpConfig',
    'django_countries',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

UPDATED 3:

Here is the outpout following python manage.py showmigrations:

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
ep
 [X] 0001_initial
sessions
 [X] 0001_initial
MaxenceP
  • 291
  • 2
  • 12
  • have you applied the migrations with `python manage.py migrate` command? – Swapnil Suryawanshi Feb 25 '20 at 12:21
  • yes, forgot to mention it, just updated the post – MaxenceP Feb 25 '20 at 12:27
  • have you added `ep` application in project's `settings.py`? – Swapnil Suryawanshi Feb 25 '20 at 12:39
  • yes it's added, i've updated the post – MaxenceP Feb 25 '20 at 12:42
  • what if you replace `ep.apps.EpConfig` to just `ep` and that too in last. – Swapnil Suryawanshi Feb 25 '20 at 12:45
  • nothing, still the same error – MaxenceP Feb 25 '20 at 12:47
  • can you post output of following command `python manage.py showmigrations` – Swapnil Suryawanshi Feb 25 '20 at 12:53
  • updated with the output following command `python manage.py showmigrations` – MaxenceP Feb 25 '20 at 13:03
  • Some database tables from the `0001_initial` migration have been created (e.g. `ep_user`) but others haven't. There's no easy way to get the database and migration files back in sync. The easiest thing to do is to rename `db.sqlite3` to `db.sqlite3.old`, the run `python manage.py migrate` which will run the migrations in a new database. – Alasdair Feb 25 '20 at 13:17
  • Well thanks @Alasdair ! It works, but if later I want to modify my models or database, is there a way to update it or will I have to redo the same thing again? – MaxenceP Feb 25 '20 at 13:38
  • try adding some other text field to user, should work now. – Swapnil Suryawanshi Feb 25 '20 at 13:40
  • If you want to make changes later, then you should 1. modify the models 2. make a migration 3. run the migration. Don't ever delete a migration file that has already been applied (I can see that you deleted `0002_address` because the `pyc` file is in your tree). I suspect you got the error because you 1. ran migration `0001_initial` 2. deleted the migration file 3. ran makemigrations, which meant that the `0001_initial` migration was different than before. – Alasdair Feb 25 '20 at 14:01

0 Answers0