TypeError: an integer is required (got type str) when running migrations or running tests
TypeError: an integer is required (got type str) when running migrations or running tests
我修改了一个现有模型如下:
原来是这样
class AppAssessment(models.Model):
contact_uuid = models.CharField(max_length=255)
step = models.IntegerField(null=True)
optionId = models.IntegerField(null=True, blank=True)
user_input = models.TextField(null=True, blank=True)
title = models.TextField(null=True, blank=True)
description = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return (
f"UUID: {self.contact_uuid} | Step: {self.step} | OptionID: {self.optionId} \n"
f"| User_Input: {self.user_input} | Title: {self.title} \n"
f"| Created_at: {self.created_at}"
)
但是我把contact_uuid改成了contact_id并且改了模型类型:
class AppAssessment(models.Model):
contact_id = models.UUIDField()
step = models.IntegerField(null=True)
optionId = models.IntegerField(null=True, blank=True)
user_input = models.TextField(null=True, blank=True)
title = models.TextField(null=True, blank=True)
description = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return (
f"UUID: {self.contact_id} | Step: {self.step} | OptionID: {self.optionId} \n"
f"| User_Input: {self.user_input} | Title: {self.title} \n"
f"| Created_at: {self.created_at}"
)
在 views.py 中,我已经将条目存储为字符串
store_url_entry = AppAssessment(contact_uuid=contact_uuid, title=title, description=description, step=step, user_input=value, optionId=optionId)
store_url_entry.save()
我删除了数据库并重新创建了它,但它仍然失败。我也重新启动了虚拟环境,但它没有帮助。
我看到的错误是这样的:
/home/osboxes/app-hub/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
No changes detected
/home/osboxes/app-hub/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Operations to perform:
Apply all migrations: app, admin, auth, authtoken, changes, contenttypes, eventstore, registrations, sessions
Running migrations:
Applying app.0021_auto_20220413_0328...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/osboxes/app-hub/venv/lib/python3.6/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 "/home/osboxes/app-hub/venv/lib/python3.6/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 "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 433, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 161, in column_sql
default_value = self.effective_default(field)
File "/home/osboxes/app-hub/venv/lib/python3.6/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 "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 793, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2333, in get_db_prep_value
value = self.to_python(value)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2343, in to_python
return uuid.UUID(**{input_form: value})
File "/usr/local/lib/python3.6/uuid.py", line 137, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
TypeError: an integer is required (got type str)
应用程序的迁移文件。0021_auto_20220413_0328:
# Generated by Django 2.2.20 on 2022-04-13 03:28
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('app', '0020_auto_20220411_1610'),
]
operations = [
migrations.RemoveField(
model_name='appassessment',
name='contact_uuid',
),
migrations.AddField(
name='contact_id',
field=models.UUIDField(default=django.utils.timezone.now),
preserve_default=False,
),
]
感谢您的帮助。
我认为问题出在您的迁移文件中...这一行....
field = models.UUIDField(default=django.utils.timezone.now)
因为您正在尝试使用 timezone.now() 填充 UUID 字段。
因此,如果此迁移文件对您不重要,只需将其删除(如果此记录存在,您还需要从数据库中删除它的记录,即 django_migrations table)或者如果这只是一个测试环境中,您可以简单地从数据库中删除所有内容并执行 makemigrations 并再迁移一次(删除此迁移文件后 -> app.0021_auto_20220413_0328
)。
重要说明:如果你想为你的 UUID 字段提供默认值,你应该使用类似的东西(在进行 makemigrations 和迁移之前):
from uuid import uuid4
contact_id = models.UUIDField(default=uuid4)
因为你的 table 中有一些记录,而你正试图向它们添加没有默认值的 UUID 字段...然后 Django 将要求默认值,恐怕没有任何当 Django 询问类似以下内容时,在默认值中提供 uuid4() 的其他方法:
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
另请注意,如果旧字段 (contact_uuid) 中的记录对您很重要,并且您想将其值添加到新字段 (contact_id),您应该编写一个自定义迁移文件这是另一个故事。
我修改了一个现有模型如下:
原来是这样
class AppAssessment(models.Model):
contact_uuid = models.CharField(max_length=255)
step = models.IntegerField(null=True)
optionId = models.IntegerField(null=True, blank=True)
user_input = models.TextField(null=True, blank=True)
title = models.TextField(null=True, blank=True)
description = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return (
f"UUID: {self.contact_uuid} | Step: {self.step} | OptionID: {self.optionId} \n"
f"| User_Input: {self.user_input} | Title: {self.title} \n"
f"| Created_at: {self.created_at}"
)
但是我把contact_uuid改成了contact_id并且改了模型类型:
class AppAssessment(models.Model):
contact_id = models.UUIDField()
step = models.IntegerField(null=True)
optionId = models.IntegerField(null=True, blank=True)
user_input = models.TextField(null=True, blank=True)
title = models.TextField(null=True, blank=True)
description = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return (
f"UUID: {self.contact_id} | Step: {self.step} | OptionID: {self.optionId} \n"
f"| User_Input: {self.user_input} | Title: {self.title} \n"
f"| Created_at: {self.created_at}"
)
在 views.py 中,我已经将条目存储为字符串
store_url_entry = AppAssessment(contact_uuid=contact_uuid, title=title, description=description, step=step, user_input=value, optionId=optionId)
store_url_entry.save()
我删除了数据库并重新创建了它,但它仍然失败。我也重新启动了虚拟环境,但它没有帮助。
我看到的错误是这样的:
/home/osboxes/app-hub/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
No changes detected
/home/osboxes/app-hub/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Operations to perform:
Apply all migrations: app, admin, auth, authtoken, changes, contenttypes, eventstore, registrations, sessions
Running migrations:
Applying app.0021_auto_20220413_0328...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/osboxes/app-hub/venv/lib/python3.6/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 "/home/osboxes/app-hub/venv/lib/python3.6/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 "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 433, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 161, in column_sql
default_value = self.effective_default(field)
File "/home/osboxes/app-hub/venv/lib/python3.6/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 "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 793, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2333, in get_db_prep_value
value = self.to_python(value)
File "/home/osboxes/app-hub/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2343, in to_python
return uuid.UUID(**{input_form: value})
File "/usr/local/lib/python3.6/uuid.py", line 137, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
TypeError: an integer is required (got type str)
应用程序的迁移文件。0021_auto_20220413_0328:
# Generated by Django 2.2.20 on 2022-04-13 03:28
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('app', '0020_auto_20220411_1610'),
]
operations = [
migrations.RemoveField(
model_name='appassessment',
name='contact_uuid',
),
migrations.AddField(
name='contact_id',
field=models.UUIDField(default=django.utils.timezone.now),
preserve_default=False,
),
]
感谢您的帮助。
我认为问题出在您的迁移文件中...这一行....
field = models.UUIDField(default=django.utils.timezone.now)
因为您正在尝试使用 timezone.now() 填充 UUID 字段。
因此,如果此迁移文件对您不重要,只需将其删除(如果此记录存在,您还需要从数据库中删除它的记录,即 django_migrations table)或者如果这只是一个测试环境中,您可以简单地从数据库中删除所有内容并执行 makemigrations 并再迁移一次(删除此迁移文件后 -> app.0021_auto_20220413_0328
)。
重要说明:如果你想为你的 UUID 字段提供默认值,你应该使用类似的东西(在进行 makemigrations 和迁移之前):
from uuid import uuid4
contact_id = models.UUIDField(default=uuid4)
因为你的 table 中有一些记录,而你正试图向它们添加没有默认值的 UUID 字段...然后 Django 将要求默认值,恐怕没有任何当 Django 询问类似以下内容时,在默认值中提供 uuid4() 的其他方法:
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
另请注意,如果旧字段 (contact_uuid) 中的记录对您很重要,并且您想将其值添加到新字段 (contact_id),您应该编写一个自定义迁移文件这是另一个故事。