尝试迁移时出现 Django 迁移错误
Django migration error while trying to migrate
前两天创建了一个app,添加了一些模型,迁移成功。因为,我没有添加或更改任何东西,但我今天添加了一些东西并尝试迁移,在进行迁移并尝试迁移之后,我得到了这个错误:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, students, user_upload
Running migrations:
Applying user_upload.0002_users_upload_phone_number...Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\aser\AppData\Roaming\Python\Python38\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\aser\AppData\Roaming\Python\Python38\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\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\aser\AppData\Roaming\Python\Python38\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\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
schema_editor.add_field(
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
self.effective_default(create_field)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\models\fields\__init__.py", line 1512, in get_db_prep_save
return connection.ops.adapt_decimalfield_value(self.to_python(value), self.max_digits, self.decimal_places)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\base\operations.py", line 523, in adapt_decimalfield_value
return utils.format_number(value, max_digits, decimal_places)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\utils.py", line 235, in format_number
value = value.quantize(decimal.Decimal(1).scaleb(-decimal_places), context=context)
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
我一直在寻找,但无法真正找到问题所在
这是我的模型
from django.db import models
from django.forms import ModelForm
class Users_Upload(models.Model):
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
email = models.CharField(max_length=120)
phone_number = models.DecimalField(max_digits=14, decimal_places=4)
address = models.CharField(max_length=120, default="Bay Area, San Francisco, California, USA")
和我的管理员
from django.contrib import admin
from .models import Users_Upload
admin.site.register(Users_Upload)
我已经解决了。我不得不清除所有迁移并再次重新迁移。
前两天创建了一个app,添加了一些模型,迁移成功。因为,我没有添加或更改任何东西,但我今天添加了一些东西并尝试迁移,在进行迁移并尝试迁移之后,我得到了这个错误:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, students, user_upload
Running migrations:
Applying user_upload.0002_users_upload_phone_number...Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\aser\AppData\Roaming\Python\Python38\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\aser\AppData\Roaming\Python\Python38\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\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\aser\AppData\Roaming\Python\Python38\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\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
schema_editor.add_field(
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
self.effective_default(create_field)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\models\fields\__init__.py", line 1512, in get_db_prep_save
return connection.ops.adapt_decimalfield_value(self.to_python(value), self.max_digits, self.decimal_places)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\base\operations.py", line 523, in adapt_decimalfield_value
return utils.format_number(value, max_digits, decimal_places)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\utils.py", line 235, in format_number
value = value.quantize(decimal.Decimal(1).scaleb(-decimal_places), context=context)
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
我一直在寻找,但无法真正找到问题所在
这是我的模型
from django.db import models
from django.forms import ModelForm
class Users_Upload(models.Model):
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
email = models.CharField(max_length=120)
phone_number = models.DecimalField(max_digits=14, decimal_places=4)
address = models.CharField(max_length=120, default="Bay Area, San Francisco, California, USA")
和我的管理员
from django.contrib import admin
from .models import Users_Upload
admin.site.register(Users_Upload)
我已经解决了。我不得不清除所有迁移并再次重新迁移。