我有这个自定义User
模型,其中我将primary_key更改为email
字段,如下所示:
class User(AbstractBaseUser, PermissionsMixin):
# Primary Key of my model
email = models.EmailField(max_length=254, primary_key=True)
username = models.CharField(_('username'),max_length=30,
unique=True,
help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[
RegexValidator(
r'^[\w.ñ@+-]+$',
_('Enter a valid username. This value may contain only '
'letters, numbers ' 'and @/./+/-/_ characters.')
),
],
error_messages={
'unique': _("A user with that username already exists."),
},
)
first_name = models.CharField(max_length=50,blank=True, null=True,
)
last_name=models.CharField(max_length=50, blank=True, null=True,)
is_staff = models.BooleanField(
default=True,
help_text='Designates whether the user can log into this admin site.')
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
我还有另一个名为Match
的模型:
class Match(models.Model):
match_date = models.DateTimeField(default=timezone.now, null=True)
home_team_players_accept = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='home_team_players_accept',
blank=True,)
away_team_players_accept = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='away_team_players_accept',
blank=True,)
home_team_players_cancel = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='home_team_players_cancel',
blank=True,)
away_team_players_cancel = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='away_team_players_cancel',
blank=True,)
fichaje_players_match = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='fichaje_players_match',
blank=True,)
当我执行python manage.py migrate
时,我得到了这个输出:
File "/home/bgarcial/.virtualenvs/fuupbol2/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.InternalError: cannot drop constraint auth_user_pkey on table auth_user because other objects depend on it
DETAIL: constraint auth_user_groups_user_id_6a12ed8b_fk_auth_user_username on table auth_user_groups depends on index auth_user_pkey
constraint auth_user_user_permissio_user_id_a95ead1b_fk_auth_user_username on table auth_user_user_permissions depends on index auth_user_pkey
constraint games_information_match__user_id_246b2ea3_fk_auth_user_username on table games_information_match_away_team_players_cancel depends on index auth_user_pkey
constraint games_information_match__user_id_9d9f8df1_fk_auth_user_username on table games_information_match_home_team_players_cancel depends on index auth_user_pkey
constraint games_information_match__user_id_79122347_fk_auth_user_username on table games_information_match_fichaje_players_match depends on index auth_user_pkey
constraint games_information_match__user_id_54e7681b_fk_auth_user_username on table games_information_match_away_team_players_accept depends on index auth_user_pkey
constraint games_information_match__user_id_14203632_fk_auth_user_username on table games_information_match_home_team_players_accept depends on index auth_user_pkey
HINT: Use DROP ... CASCADE to drop the dependent objects too.
我发现了一些关于这种情况的参考,但我不明白如何解决这个不便
Renaming a primary key fails "cannot drop constraint on table because other objects depend on it"
Cannot drop table users because other objects depend on it
我甚至尝试删除整个数据库,但不便之处仍然存在。
答案 0 :(得分:1)
在项目的migrations
文件夹中,有一些名称如下的文件:
0001_initial.py
0002_auto_ ... .py etc.
这些文件是数据库迁移的历史记录,因为您刚刚删除了数据库,我将假设您不需要迁移历史记录。
因此,简单的解决方案是清除migrations
文件夹(删除所有除 __init__.py
文件除外),再次删除数据库并执行以下操作:
python manage.py makemigrations
python manage.py migrate
祝你好运:)