“TypeError:应用迁移时需要一个整数(获得类型str)”

时间:2017-11-10 18:24:35

标签: python django uuid migrate django-migrations

添加一些模型后,我使用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字段,但我无法将其放在上面,因为我在回溯中没有看到对我的迁移文件的引用。

您是否知道我应该在哪些方面解决问题?它可能来自哪里?

1 个答案:

答案 0 :(得分:1)

问题是由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&gt; = 1.9似乎是一个特殊的错误消息。为了您的兴趣,在Django 1.8中没有检查或调整任何内容,可以保存无效的UUID。使用隐式主键id时,会显示错误消息:int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'。没有什么需要向Django报告。