Makemigrations is not making all migrations

I have several apps inside a project:

Post
Poll
Users

I have deleted all tables in the database. I have deleted all migration files and I have deleted all pycache directories inside the app folders.

When I run makemigrations only ONE model (Post) migration file gets created and I have to manually go and create the other migration files using makemigrations modelname.

Why is this happening please?

it happend because you dalete migrations folder and __init__.py inside it.
I don’t really know the exact detail, but I think django need that folder internally to create migrations file.

if you want to reset the migrations, just delete file inside migrations folder that have number on the beginning of file name (ex: 0001_initial.py , etc). and delete all data in your table.

Yeah I did not delete the migrations folder. I only deleted the files inside it (not init.py) and I deleted the pycache directories

sorry there, should be asking first. :grinning:
did you already delete the table?
if you using sqlite3, try deleting the file

As in my original post

I can’t reproduce this issue.

I already deleting all migrations file that has file name started with number.
Then deleting my sqlite3 file.
Then I run python manage.py makemigrations.
Then all app successfully creating migrations file.

Usually this issue happend when migrations folder deleted in app.

maybe you can add more details (step by step) how you try to reset migrations and finally got that issue.

The steps are exactly as I put it there not sure what else to tell you. I am not using sqlite but mysql

If you’ve deleted all tables, including Django’s “internal” tables, I’d suggest running migrate first. That should create the initial set of tables needed by Django to function. Then you can try running makemigrations again.

Thank you Ken I did not think about doing that. I know you must run migrate when you start a project, but I did not think in doing it with this. Again thank you

I did what you suggested and after deleting the database and all existing migration files I ran migrate and got an error

Here is the traceback:

Operations to perform:
  Apply all migrations: admin, auth, authtoken, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying admin.0001_initial...Traceback (most recent call last):
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1824, "Failed to open the referenced table 'users'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 349, in handle
    post_migrate_state = executor.migrate(
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/migrations/executor.py", line 255, in apply_migration
    migration_recorded = True
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 164, in __exit__
    self.execute(sql)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 199, in execute
    cursor.execute(sql, params)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/utils.py", line 103, in execute
    return super().execute(sql, params)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1824, "Failed to open the referenced table 'users'")

If I create the migration files manually then it works. How do I fix this pls?

That looks like you might have dropped one of the database system tables.

At this point, I’d suggest you drop the database and recreate it.

Hi Ken

How is it possible for me to drop system tables when I empty the database? Any case I deleted the database and recreated it and the same error. It is not creating the migrations files.

The problem is with my user model, because if I do python manage.py makemigrations users first and then I run migrate then everything works fine.

Sorry no it does not create my other models’ migration files either. Just the systems tables and the users table.

Please describe in detail your project directory layout. (Just the directories, I don’t need to see all the individual files.)

Identify what versions of Python and Django you are using.

Also post the INSTALLED_APPS section of your settings file.

Finally, examine your source code files to identify if you have any database-related operations (queries, inserts, etc) at the “module” layer of a file. (Meaning that that code is not in a function.)

Ok my dir layout is as follows:

backend (containing folder)
/ cooking (project folder)
/ api (app)
/ pages (app)
/ posts (app)
/ storage (static files)
/ users (app)
/ usersettings (app)

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",

    # THIRD PARTY APPS
    "rest_framework",
    "rest_framework.authtoken",
    "drf_yasg",
    "corsheaders",
    
    # CUSTOM APPS
    "api",
    "users",
    "posts",
    "usersettings",
]

Using:

Python 3.8
Django 4.1

When you deleted the files from the migrations directory, did you also delete the __init__.py files? If so, you’ll need them in there. (They can be empty files (0 length) but they must exist.)

After a struggle I finally figured it out. Because I have a custom user model I cannot just run migrate. I must first make a migration for my custom user model and then I can run migrate.

Thanks for the help