Hi,
My app really is just creating a small web site to add users and figure out how to do CRUD operations in building user profiles. I am using the following setup:
Django Version: 2.0.3
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'auditlog',
'widget_tweaks',
'Members.apps.MembersConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'auditlog.middleware.AuditlogMiddleware']
I am using sqlite3 as the DB backend..
I can create the virgin project, tweak the settings file and do the initial "makemigrations" and "migrate". This, of course, creates the Django "user" table(s). I went about creating my own "profile" model that "extends" the User model by creating a "oneToOne" field that points back to User and specifies an "on_delete=models.CASCADE" clause:
class Profile(models.Model):
...
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
blank=False,
null=False,
)
...
The thing is the table that is created is given the constraint:
FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) DEFERRABLE INITIALLY DEFERRED,
but the "on delete cascade" clause is missing.
I first noticed this when testing a profile delete operation. I get a foreign key constraint violation. Looking into that led me here to you guys.
I have added that clause to the table:
FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) on delete cascade DEFERRABLE INITIALLY DEFERRED,
but I still get the constraint violation. I did more digging last night and see that at least one of the Django generated "user_*" tables also has a foreign key relationship back to the "user" table and that is also missing the cascade clause.
My guesses at this instant include:
- I have no idea what I am doing
- Django or the sqlite3 backend *should* be handling the cascade ops internally -- but isn't
What am I missing?