'Django: Makemigration Error "models.E028" and "models.E340" after extending Authuser (legacy MySQL)
I connected my Django project to a legacy MySQL DB and did a first migration.
I then extended the AuthUser model with a "NewUser" model (made sure that AUTH_USER_MODEL settings.py to reference 'appname.NewUser')
When running makemigrations I get these error messages:
models.E028
account_emailaddress: (models.E028) db_table 'account_emailaddress' is used by multiple models: seasonscan_api.AccountEmailaddress, account.EmailAddress.
account_emailconfirmation: (models.E028) ...(same error)
auth_group: (models.E028) ...(same error)
auth_permission: (models.E028) ...(same error)
django_admin_log: (models.E028) ...(same error)
django_content_type: (models.E028) ...(same error)
django_session: (models.E028) ...(same error)
django_site: (models.E028) ...(same error)
socialaccount_socialaccount: (models.E028) ...(same error)
socialaccount_socialapp: (models.E028) ...(same error)
socialaccount_socialtoken: (models.E028) ...(same error)
fields.E340
auth.Group.permissions: (fields.E340) The field's intermediary table 'auth_group_permissions' clashes with the table name of 'seasonscan_api.AuthGroupPermissions'.
socialaccount.SocialApp.sites: (fields.E340) ...(same error)
I checked the docs which didnt show more than the error message without additional info. Any advice would help me a lot.
Here the models as reference:
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db.models.signals import post_save
class AuthGroup(models.Model):
name = models.CharField(unique=True, max_length=150)
class Meta:
managed = True
db_table = 'auth_group'
class AuthGroupPermissions(models.Model):
id = models.BigAutoField(primary_key=True)
group = models.ForeignKey('AuthGroup', on_delete=models.CASCADE, related_name='authgroup_authgrouppermissions')
permission = models.ForeignKey('AuthPermission', on_delete=models.CASCADE, related_name='authpermission_authgrouppermissions')
class Meta:
managed = True
db_table = 'auth_group_permissions'
unique_together = (('group', 'permission'),)
class AuthPermission(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey('DjangoContentType', on_delete=models.CASCADE, related_name='djangocontenttype_authpermissions')
codename = models.CharField(max_length=100)
class Meta:
managed = True
db_table = 'auth_permission'
unique_together = (('content_type', 'codename'),)
class AuthUserGroups(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.ForeignKey('NewUser', on_delete=models.CASCADE, related_name='newuser_authusergroups')
group = models.ForeignKey('AuthGroup', on_delete=models.CASCADE, related_name='authgroup_authusergroups')
class Meta:
managed = True
db_table = 'auth_user_groups'
unique_together = (('user', 'group'),)
class AuthUserUserPermissions(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.ForeignKey('NewUser', on_delete=models.CASCADE, related_name='newuser_authuseruserpermissions')
permission = models.ForeignKey('AuthPermission', on_delete=models.CASCADE, related_name='authpermission_authuseruserpermissions')
class Meta:
managed = True
db_table = 'auth_user_user_permissions'
unique_together = (('user', 'permission'),)
class DjangoAdminLog(models.Model):
action_time = models.DateTimeField()
object_id = models.TextField(blank=True, null=True)
object_repr = models.CharField(max_length=200)
action_flag = models.PositiveSmallIntegerField()
change_message = models.TextField()
content_type = models.ForeignKey('DjangoContentType', on_delete=models.CASCADE, blank=True, null=True, related_name='djangocontenttype_djangoadminlog')
user = models.ForeignKey('NewUser', on_delete=models.CASCADE, related_name='newuser_djangoadminlog')
class Meta:
managed = True
db_table = 'django_admin_log'
class DjangoContentType(models.Model):
app_label = models.CharField(max_length=100)
model = models.CharField(max_length=100)
class Meta:
managed = True
db_table = 'django_content_type'
unique_together = (('app_label', 'model'),)
class DjangoMigrations(models.Model):
id = models.BigAutoField(primary_key=True)
app = models.CharField(max_length=255)
name = models.CharField(max_length=255)
applied = models.DateTimeField()
class Meta:
managed = True
db_table = 'django_migrations'
class DjangoSession(models.Model):
session_key = models.CharField(primary_key=True, max_length=40)
session_data = models.TextField()
expire_date = models.DateTimeField()
class Meta:
managed = True
db_table = 'django_session'
class AccountEmailaddress(models.Model):
email = models.CharField(unique=True, max_length=254)
verified = models.IntegerField()
primary = models.IntegerField()
user = models.ForeignKey('NewUser', on_delete=models.CASCADE)
class Meta:
managed = True
db_table = 'account_emailaddress'
class AccountEmailconfirmation(models.Model):
created = models.DateTimeField()
sent = models.DateTimeField(blank=True, null=True)
key = models.CharField(unique=True, max_length=64)
email_address = models.ForeignKey(AccountEmailaddress, on_delete=models.CASCADE)
class Meta:
managed = True
db_table = 'account_emailconfirmation'
class DjangoSite(models.Model):
domain = models.CharField(unique=True, max_length=100)
name = models.CharField(max_length=50)
class Meta:
managed = True
db_table = 'django_site'
class SocialaccountSocialaccount(models.Model):
provider = models.CharField(max_length=30)
uid = models.CharField(max_length=191)
last_login = models.DateTimeField()
date_joined = models.DateTimeField()
extra_data = models.TextField()
user = models.ForeignKey('NewUser', on_delete=models.CASCADE)
class Meta:
managed = True
db_table = 'socialaccount_socialaccount'
unique_together = (('provider', 'uid'),)
class SocialaccountSocialapp(models.Model):
provider = models.CharField(max_length=30)
name = models.CharField(max_length=40)
client_id = models.CharField(max_length=191)
secret = models.CharField(max_length=191)
key = models.CharField(max_length=191)
class Meta:
managed = True
db_table = 'socialaccount_socialapp'
class SocialaccountSocialappSites(models.Model):
id = models.BigAutoField(primary_key=True)
socialapp = models.ForeignKey(SocialaccountSocialapp, on_delete=models.CASCADE)
site = models.ForeignKey(DjangoSite, on_delete=models.CASCADE)
class Meta:
managed = True
db_table = 'socialaccount_socialapp_sites'
unique_together = (('socialapp', 'site'),)
class SocialaccountSocialtoken(models.Model):
token = models.TextField()
token_secret = models.TextField()
expires_at = models.DateTimeField(blank=True, null=True)
account = models.ForeignKey(SocialaccountSocialaccount, on_delete=models.CASCADE)
app = models.ForeignKey(SocialaccountSocialapp, on_delete=models.CASCADE)
class Meta:
managed = True
db_table = 'socialaccount_socialtoken'
unique_together = (('app', 'account'),)
###
### user and membership, subcription and payment related
class CustomAccountManager(BaseUserManager):
def create_superuser(self, email, user_name, first_name, last_name, password, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
if other_fields.get('is_staff') is not True:
raise ValueError('Superuser must be assigned to is_staff=True.')
if other_fields.get('is_superuser') is not True:
raise ValueError('Superuser must be assigned to is_superuser=True.')
return self.create_user(email, user_name, first_name, last_name, password, **other_fields)
def create_user(self, email, user_name, first_name, last_name, password, **other_fields):
if not email:
raise ValueError(_("You must provide an email address"))
if not user_name:
raise ValueError(_("You must provide a user name"))
if not password:
raise ValueError(_("You must provide a password"))
email = self.normalize_email(email)
user = self.model(email=email, user_name=user_name, first_name=first_name, last_name=last_name, **other_fields)
user.set_password(password)
user.save()
return user
class NewUser(AbstractBaseUser):
password = models.CharField(_('password'),max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
username = models.CharField(_('username'), unique=True, max_length=150)
first_name = models.CharField(_('first name'), max_length=150)
last_name = models.CharField(_('last name'), max_length=150)
email = models.EmailField(_('email address'), unique=True)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_contributor = models.BooleanField(default=False)
is_extension_user = models.BooleanField(default=False)
is_developer_user = models.BooleanField(default=False)
is_newsletter_member = models.BooleanField(default=True)
is_active = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
objects = CustomAccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['user_name', 'first_name', 'last_name']
class Meta:
managed = True
db_table = 'new_user'
def __str__(self):
return self.user_name
app-specific models continuing below...
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
