首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:运行迁移或运行测试时需要一个整数(got类型str)

TypeError:运行迁移或运行测试时需要一个整数(got类型str)
EN

Stack Overflow用户
提问于 2022-04-13 04:33:50
回答 1查看 265关注 0票数 0

我修改了一个现有的模式如下:

原来是这样的

代码语言:javascript
复制
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,并更改了模型类型:

代码语言:javascript
复制
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中,我已经将条目存储为字符串。

代码语言:javascript
复制
        store_url_entry = AppAssessment(contact_uuid=contact_uuid, title=title, description=description, step=step, user_input=value, optionId=optionId)
        store_url_entry.save()

我已经删除了数据库并重新创建了它,但是它仍然失败。我还重新启动了虚拟env,但是没有帮助。

我看到的错误是:

代码语言:javascript
复制
  /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)

App.0021_auto_20220413_0328的迁移文件:

代码语言:javascript
复制
# 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,
        ),
    ]

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-13 07:52:52

我认为问题来自于你的移民文件..。这条线..。

代码语言:javascript
复制
field = models.UUIDField(default=django.utils.timezone.now)

因为您试图用timezone.now()填充UUID字段。因此,如果这个迁移文件对您不重要,只需删除它(如果存在此记录,也需要从数据库中删除它的记录,即django_migrations表),或者如果这只是一个测试环境,您可以简单地删除数据库中的所有内容,然后进行迁移和再迁移一次(在删除这个迁移文件-> app.0021_auto_20220413_0328之后)。

重要注意事项:如果您想为您的UUID字段提供一个默认值,您应该使用类似的东西(在进行制造和迁移之前):

代码语言:javascript
复制
from uuid import uuid4

contact_id = models.UUIDField(default=uuid4)

因为您的表中有一些记录,并且您试图在没有默认值的情况下向它们添加UUID字段.然后Django将请求默认值,而且当Django询问以下内容时,恐怕没有任何其他方法在默认值中提供uuid4():

代码语言:javascript
复制
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)中,则应该编写一个自定义迁移文件,这是另一个故事。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71851934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档