python - django.db.utils.OperationalError: duplicate column name: Email
问题描述
I want to create a Django app that supports two different kinds of users. For that I have first extended my User by inheriting AbstractUser. Some fields are common to both the types of Users that I want my Django app to support and hence I add those fields in the extended User model.
My User model is as follows.
class User(AbstractUser):
is_a = models.BooleanField(default=False) # a boolean flag to determine if this user is of type a
is_b = models.BooleanField(default=False) # a boolean flag to determine if this user is of type b
Name = models.CharField(max_length=128)
Email = models.EmailField()
MobileNumber = PhoneNumberField(null=False, blank=False, unique=True)
Age = models.PositiveIntegerField()
Gender = models.CharField(max_length=1, choices=GenderChoices)
Organisation = models.CharField(max_length=128)
Designation = models.CharField(max_length=128)
def __str__(self):
return self.Name
I then make migrations. This is my first ever migration in this project. My migrations file is as follows:
# 0001_initial.py
# Generated by Django 2.0.1 on 2020-10-01 17:30
from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import phonenumber_field.modelfields
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0009_alter_user_last_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('is_a', models.BooleanField(default=False)),
('is_b', models.BooleanField(default=False)),
('Name', models.CharField(max_length=128)),
('Email', models.EmailField(max_length=254)),
('MobileNumber', phonenumber_field.modelfields.PhoneNumberField(max_length=128, region=None, unique=True)),
('Age', models.PositiveIntegerField()),
('Gender', models.CharField(choices=[('M', 'MALE'), ('F', 'FEMALE')], max_length=1)),
('Organisation', models.CharField(max_length=128)),
('Designation', models.CharField(max_length=128)),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
# etc. (irrelevant)
When I run 'migrate' after performing 'makemigrations', I get the following error.
Operations to perform:
Apply all migrations: admin, auth, beergame, contenttypes, sessions
Running migrations:
Applying beergame.0001_initial...Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 301, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: Email
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 198, in handle
post_migrate_state = executor.migrate(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 298, in create_model
self.execute(sql, params or None)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 117, in execute
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 301, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: duplicate column name: Email
Django says that there is a duplicate Email column. But I don't have any duplicate according to my model and my migrations .py file. How do I fix this?
解决方案
Found out the answer.
So as pointed out by @Willem Van Onsem in the comments, the column names are case insensitive. A clash happened between the User model's email column and the Email column I had specified. It can be resolved by either removing the Email column from the models, or by renaming it to something else like EmailAddress.
推荐阅读
- angular - 对 HttpClient 结果进行长轮询并流式传输到 CSV 文件中
- php - 用本地语言的 PHP 函数替换为 Unicode 的更改位置
- python - 更改嵌套列表中的特定条目
- python - 尝试在 kivy 核心图像中检索和显示时出现 UnicodeDecodeError
- vb.net - VB.net 舍入问题
- rest - 通过rest api 2.2版在tfs 2015中创建测试用例
- asp.net-mvc - VSTS MSDeploy 深层嵌套路径
- c# - 在 ASP.NET Core 2.1 中的应用程序启动时调用非静态类来种子数据库
- c# - 调用通用对象列表作为参数
- node.js - 根据节点版本有条件地导入节点模块