Reputation: 283
I'm working on a django project with Django 2.2 and Postgresql 10.11 as database with latest version of psycopg2. But when I run migrate command, below error happen for specific migration.
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'
Here is the migration file that this error happened on:
# 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(),
),
]
And this is the model that migration belongs to:
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()
Update
This the last migration that includes 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)),
],
),
]
Anyone knows where is the problem?
Upvotes: 0
Views: 269
Reputation: 509
You seem to have mixed up the on_delete
parameter for foreign keys with the default
value for other fields.
If you want to add a default value null to the field api_key
, alter them both in models and your previous migration file:
# in models.py
api_key = models.TextField(default=None, null=True)
# in 0002_auto_20191007_1655
('api_key', models.TextField(default=None, null=True)),
Or, if you have not yet migrated your files, delete both 0002_auto_20191007_1655
and 0003_auto_20191007_1700
and run makemigrate/migrate again.
Upvotes: 1