gpt4 book ai didi

python - 应用迁移时为 "TypeError: an integer is required (got type str)"

转载 作者:行者123 更新时间:2023-12-01 02:27:28 24 4
gpt4 key购买 nike

添加一些模型后,我使用 makemigrations 生成迁移:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-10 18:10
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import uuid


class Migration(migrations.Migration):

dependencies = [
('restaurants', '0011_auto_20171024_1428'),
('shop', '0003_auto_20171110_1505'),
]

operations = [
migrations.CreateModel(
name='Customer',
fields=[
('email', models.CharField(max_length=255, unique=True)),
('newsletter', models.BooleanField(default=False)),
('key', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='clé')),
],
),
migrations.CreateModel(
name='Order',
fields=[
('number_of_guests', models.PositiveSmallIntegerField()),
('key', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='clé')),
('date_created', models.DateTimeField(auto_now_add=True)),
('date_changed', models.DateTimeField(auto_now=True)),
('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Customer')),
('deal', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='restaurants.Deal')),
],
),
migrations.AddField(
model_name='payment',
name='date_created',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='payment',
name='stripe_token',
field=models.CharField(default=django.utils.timezone.now, max_length=5000),
preserve_default=False,
),
migrations.AddField(
model_name='payment',
name='order',
field=models.ForeignKey(default=django.utils.timezone.now, on_delete=django.db.models.deletion.CASCADE, to='shop.Order'),
preserve_default=False,
),
]

这看起来不错,但是当我运行迁移时,它失败并出现以下回溯:

Applying shop.0004_auto_20171110_1910...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "{virtualenv}\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
utility.execute()
File "{virtualenv}\lib\site-packages\django\core\management\__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "{virtualenv}\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "{virtualenv}\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "{virtualenv}\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
fake_initial=fake_initial,
File "{virtualenv}\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "{virtualenv}\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "{virtualenv}\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "{virtualenv}\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "{virtualenv}\lib\site-packages\django\db\migrations\operations\fields.py", line 87, in database_forwards
field,
File "{virtualenv}\lib\site-packages\django\db\backends\sqlite3\schema.py", line 238, in add_field
self._remake_table(model, create_field=field)
File "{virtualenv}\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table
self.effective_default(create_field)
File "{virtualenv}\lib\site-packages\django\db\backends\base\schema.py", line 229, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "{virtualenv}\lib\site-packages\django\db\models\fields\related.py", line 963, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "{virtualenv}\lib\site-packages\django\db\models\fields\__init__.py", line 770, in get_db_prep_save
prepared=False)
File "{virtualenv}\lib\site-packages\django\db\models\fields\__init__.py", line 2387, in get_db_prep_value
value = self.to_python(value)
File "{virtualenv}\lib\site-packages\django\db\models\fields\__init__.py", line 2396, in to_python
return uuid.UUID(value)
File "c:\python36\Lib\uuid.py", line 137, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
TypeError: an integer is required (got type str)

问题似乎来自 UUID 字段,但我无法确定它,因为我在回溯中没有看到对我的迁移文件的引用。

您知道我应该在哪里解决这个问题吗?它可能来自哪里?

最佳答案

该问题是由ForeignKey 中的默认值类型无效引起的:

class Payment(models.Model):
...
order = models.ForeignKey(to='shop.Order', on_delete=django.db.models.deletion.CASCADE,
default=django.utils.timezone.now)
...

您通常不应为外键分配默认值,最终应分配与相关主键兼容的值,并且切勿分配像 datetime 这样的无效类型。 .

字段来源是从最后的AddField推导出来的在迁移中。这种不明确的错误消息似乎是 UUIDField 和 Django >= 1.9 的特例。为了您的利益,Django 1.8 中没有检查或调整任何内容,并且可以保存无效的 UUID。使用隐式主键 id ,错误消息清晰可见:int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime' 。无需向 Django 报告任何内容。

关于python - 应用迁移时为 "TypeError: an integer is required (got type str)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com