Reputation: 226
I'm making a Django project with Django 3.1.
I was trying to rewrite the User
class in Django. This is what I wrote in app_questions/models.py
:
import django.contrib.auth.models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
#pass
settings.py
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_questions',
]
AUTH_USER_MODEL = 'app_questions.User'
Also I created an superuser
before I did these all.
After that I did makemigrations
and migrate
in the cmd and ran the server. I open the admin page and try to edit the User
I created beforehand, but when I click the edit page, this error occured:
OperationalError at /admin/app_questions/user/1/change/
no such table: app_questions_user_groups
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/app_questions/user/1/change/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:
no such table: app_questions_user_groups
Exception Location: C:\Users\*\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable: C:\Users\*\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.4
Python Path:
['C:\\Users\\*\\django_sample\\vitural_env\\zeaf',
'C:\\Users\\*\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\*\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\*\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\*\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\*\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
I found a few posts to solve this. These are methods I tried:
makemigrations
and migrate
(same error)migrate (appname) --fake
(same error)User
class (error in /admin: no such column
)migrate --sync-db
(same error)migrate --run-syncdb
(same error)$ manage.py (appname) zero
(failed: Unknown command
)migrate --fake (appname) zero
(same error)They all didn't solve the problem. Posts in 2016 or 2017 seems doesn't work anymore for now.
Any help is appreciated & sorry if this is a duplicated post.
Upvotes: 2
Views: 1905
Reputation: 54
If i right understood, you had already your project running, that is the makemigration is not the first makemigration. If so try delete the database and run makemigration again, maybe in a test dev environment, just to check the problem. If this work (and should) you've found the problem. If you don't want to delete the database, maybe you could extend user instead of overwriting.
Upvotes: 1
Reputation: 2451
When you run the first migrate
command, all the default migrations would come into effect which means all the tables and relationships would be created.
When you are customizing the default User
(overriding the default User
model and providing a value to AUTH_USER_MODEL
) all the relationships would change.
Changing AUTH_USER_MODEL after you’ve created database tables is significantly more difficult since it affects foreign keys and many-to-many relationships
Changing the user model mid-way is not recommended as you need to manually fix your database schema.
The simplest way to fix this, if you changed the User model mid-way would be: deleting all the migrations, database and running makemigrations and migrate commands. Which created a fresh database schema.
This is a limitation which is said in the docs as:
Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.
From Docs:
The best practice is to create the custom User model at the start of the project and then migrate and work on the User model later in case anything related to the authentication or User model are to be changed later
Upvotes: 1
Reputation: 1269
This error usually means that something has gone wrong with your migrations, and often happens when you try to make changes like these after the fact. One possible solution is to delete all your migration files except for the one ending in initial.py
. Then makemigrations
and migrate
again and it should work.
Upvotes: 1