使用 Postgresql 数据库在 Django 中使用 运行 迁移命令时出错
Error While running migrate command in Django with Postgresql database
我正在开发一个 Django 项目,使用 Django 2.2 和 Postgresql 10.11 作为最新版本 的数据库psycopg2。
但是当我 运行 迁移命令时,特定迁移会发生以下错误。
Running migrations:
Applying panelprofile.0003_auto_20191007_1700...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 "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\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 "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\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 "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 535, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\postgresql\schema.py", line 122, in _alter_field
new_db_params, strict,
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 648, in _alter_field
old_default = self.effective_default(old_field)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 212, in _effective_default
default = field.get_default()
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\models\fields\__init__.py", line 797, in get_default
return self._get_default()
TypeError: SET_NULL() missing 4 required positional arguments: 'collector', 'field', 'sub_objs', and 'using'
这是发生此错误的迁移文件:
# Generated by Django 2.2 on 2019-10-07 13:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('panelprofile', '0002_auto_20191007_1655'),
]
operations = [
migrations.AlterField(
model_name='smspanelinfo',
name='api_key',
field=models.TextField(),
),
]
这是迁移所属的模型:
class SMSPanelInfo(models.Model):
businessman = models.OneToOneField(Businessman, on_delete=models.CASCADE)
username = models.CharField(max_length=20)
api_key = models.TextField()
STATUS_CHOICES = [('1', 'ACTIVE_LOGIN'), ('0', 'INACTIVE'), ('2', 'ACTIVE')]
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='0')
minimum_allowed_credit = models.PositiveIntegerField(default=10000)
credit = models.PositiveIntegerField(default=1000)
sms_farsi_cost = models.PositiveSmallIntegerField()
sms_english_cost = models.PositiveIntegerField()
def deactivate(self):
"""
deactivates sms panel of the user in kavenegar
:return:
"""
ClientManagement().deactivate_sms_panel(self.api_key)
self.status = SMSPanelStatus.INACTIVE
self.save()
def activate(self):
"""
activates sms panel on kavenegar
:return:
"""
ClientManagement().activate_sms_panel(self.api_key)
self.status = SMSPanelStatus.ACTIVE_LOGIN
self.save()
def create_sms_panel(self, user: Businessman, password: str):
client = ClientManagement()
info = client.add_user(user, password)
info.businessman = user
info.save()
return info
def update_panel_info(self):
client = ClientManagement()
client.fetch_user(self.businessman)
self.api_key = info.api_key
self.credit = info.credit
self.sms_farsi_cost = info.sms_farsi_cost
self.sms_english_cost = info.sms_english_cost
self.save()
def reduce_credit(self, amount: int):
self.credit -= amount
self.save()
更新
这是包含 api_key
的最后一次迁移
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='SMSPanelInfo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=20)),
('api_key', models.TextField(default=django.db.models.deletion.SET_NULL)),
('status', models.CharField(choices=[('1', 'ACTIVE_LOGIN'), ('0', 'INACTIVE'), ('2', 'ACTIVE')], default='0', max_length=1)),
('minimum_allowed_credit', models.PositiveIntegerField(default=10000)),
('credit', models.PositiveIntegerField(default=1000)),
('sms_farsi_cost', models.PositiveSmallIntegerField()),
('sms_english_cost', models.PositiveSmallIntegerField()),
('businessman', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
谁知道问题出在哪里?
您似乎混淆了外键的 on_delete
参数和其他字段的 default
值。
如果您想将默认值 null 添加到字段 api_key
,请在模型和之前的迁移文件中更改它们:
# in models.py
api_key = models.TextField(default=None, null=True)
# in 0002_auto_20191007_1655
('api_key', models.TextField(default=None, null=True)),
或者,如果您尚未迁移文件,请再次删除 0002_auto_20191007_1655
和 0003_auto_20191007_1700
以及 运行 makemigrate/migrate。
我正在开发一个 Django 项目,使用 Django 2.2 和 Postgresql 10.11 作为最新版本 的数据库psycopg2。 但是当我 运行 迁移命令时,特定迁移会发生以下错误。
Running migrations:
Applying panelprofile.0003_auto_20191007_1700...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 "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\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 "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\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 "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 535, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\postgresql\schema.py", line 122, in _alter_field
new_db_params, strict,
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 648, in _alter_field
old_default = self.effective_default(old_field)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 212, in _effective_default
default = field.get_default()
File "C:\Users\Pouya\Desktop\programs\python\marketpine-backend\venv\lib\site-packages\django\db\models\fields\__init__.py", line 797, in get_default
return self._get_default()
TypeError: SET_NULL() missing 4 required positional arguments: 'collector', 'field', 'sub_objs', and 'using'
这是发生此错误的迁移文件:
# Generated by Django 2.2 on 2019-10-07 13:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('panelprofile', '0002_auto_20191007_1655'),
]
operations = [
migrations.AlterField(
model_name='smspanelinfo',
name='api_key',
field=models.TextField(),
),
]
这是迁移所属的模型:
class SMSPanelInfo(models.Model):
businessman = models.OneToOneField(Businessman, on_delete=models.CASCADE)
username = models.CharField(max_length=20)
api_key = models.TextField()
STATUS_CHOICES = [('1', 'ACTIVE_LOGIN'), ('0', 'INACTIVE'), ('2', 'ACTIVE')]
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='0')
minimum_allowed_credit = models.PositiveIntegerField(default=10000)
credit = models.PositiveIntegerField(default=1000)
sms_farsi_cost = models.PositiveSmallIntegerField()
sms_english_cost = models.PositiveIntegerField()
def deactivate(self):
"""
deactivates sms panel of the user in kavenegar
:return:
"""
ClientManagement().deactivate_sms_panel(self.api_key)
self.status = SMSPanelStatus.INACTIVE
self.save()
def activate(self):
"""
activates sms panel on kavenegar
:return:
"""
ClientManagement().activate_sms_panel(self.api_key)
self.status = SMSPanelStatus.ACTIVE_LOGIN
self.save()
def create_sms_panel(self, user: Businessman, password: str):
client = ClientManagement()
info = client.add_user(user, password)
info.businessman = user
info.save()
return info
def update_panel_info(self):
client = ClientManagement()
client.fetch_user(self.businessman)
self.api_key = info.api_key
self.credit = info.credit
self.sms_farsi_cost = info.sms_farsi_cost
self.sms_english_cost = info.sms_english_cost
self.save()
def reduce_credit(self, amount: int):
self.credit -= amount
self.save()
更新
这是包含 api_key
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='SMSPanelInfo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=20)),
('api_key', models.TextField(default=django.db.models.deletion.SET_NULL)),
('status', models.CharField(choices=[('1', 'ACTIVE_LOGIN'), ('0', 'INACTIVE'), ('2', 'ACTIVE')], default='0', max_length=1)),
('minimum_allowed_credit', models.PositiveIntegerField(default=10000)),
('credit', models.PositiveIntegerField(default=1000)),
('sms_farsi_cost', models.PositiveSmallIntegerField()),
('sms_english_cost', models.PositiveSmallIntegerField()),
('businessman', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
谁知道问题出在哪里?
您似乎混淆了外键的 on_delete
参数和其他字段的 default
值。
如果您想将默认值 null 添加到字段 api_key
,请在模型和之前的迁移文件中更改它们:
# in models.py
api_key = models.TextField(default=None, null=True)
# in 0002_auto_20191007_1655
('api_key', models.TextField(default=None, null=True)),
或者,如果您尚未迁移文件,请再次删除 0002_auto_20191007_1655
和 0003_auto_20191007_1700
以及 运行 makemigrate/migrate。