After experimenting with setting up CustomUser and many-to-many relationships, I decided to make some (what turned out to be major) changes to the models. I had read that CustomUser/Auth needs to be set up first, or else it'll be a mess. But since I'm just learning and there was barely any data involved, I just went for it--not to mess up the CustomUser model intentionally, but I changed a model name--and (maybe this is the critical error) I updated the names everywhere it appeared. Now I'd deleted and reset the database, VENV folder, all the migration (along with __pycache__
) files, and recreated test data so many times that I thought I could download the GitHub repo and set up a fresh new app easily again. But the app refuses to run and keeps asking for a missing table.
Re-creating the app isn't my main concern-I'd like to understand what happened-what is this missing table (see the snippet of the error code)? AND: Why is a perfectly intact repo having the same issue with the missing table?
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Volumes/Volume2/dev/lms6-v2/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/Volumes/Volume2/dev/lms6-v2/venv/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: accounts_staff
The app is called accounts
and the CustomUser models are:
class CustomUser(AbstractUser):
user_type_data = ((1, "HOD"), (2, "Staff"), (3, "Student"))
user_type = models.PositiveSmallIntegerField(default=1, choices=user_type_data)
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
class AdminHOD(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
class Staff(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
user_type = "Staff"
email = models.EmailField()
address = models.TextField(blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
note = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
def __str__(self):
return self.admin.first_name " " self.admin.last_name
def get_absolute_url(self):
return reverse('staff_detail', kwargs={'pk': self.pk})
CodePudding user response:
first of all u need to delete all your migrations files(sure u have ) and the (pycache) ...
After you have done that dont just run makemigration u have to be logical with it
- run python manage.py makemigrations <app_name:the app that contains the AUTH_USER_MODEL>
- run python manage.py makemigrations <app_name> but this time u decide what app comes next in the way u wrote ur models they makemigrations in that order' 3)run python manage.py makemigrations 4)lastly run python manage.py migrations
... this how i solved this issue anytime i run into this problems
CodePudding user response:
Obviously messing with the CustomUser model in Django isn't to be attempted casually, but this was a good learning experience. So for the newly initiated accidentally stuck at the same spot, the answer by Ogechuwku Matthew is a good start.
To elaborate-this is what I did:
- Deleted all the migration files, including files in
__pycache__
(EXCEPT for the__init__
file(s). - Disabled the
AUTH_USER_MODEL = 'myapp.MyCustomUser'
line insettings.py
This step is key. - Removed (or commented out) all but the most essential files.
- Commented out all but the
CustomUser
model and models extended from it. - Ran
python manage.py makemigrations <app_name>
and then 'python manage.py migrate` - After step 4 worked, ran
python manage.py makemigrations <app_name>
andpython manage.py migrate
again. - Moved the rest of the files back in, starting with the simplest bits, like 'HomeView' in
views.py
and 'home.html'. - When the app started up with 'home.html', un-commented models, views, and urls one cluster at a time.
Additionally: If error occur when running the app, first check the embedded dynamic links--they may extend
or include
files that aren't there yet-same with {% load static %}