Brachamul
Brachamul

Reputation: 1974

"TypeError: an integer is required (got type str)" when applying migrations

After adding some models, I used makemigrations to generate a migration :

# -*- 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,
        ),
    ]

This seems fine, but when I run the migration, it fails with the following traceback :

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)

The issue seems to come from the UUID fields, but I can't place my finger on it, as I don't see a reference to my migration file in the traceback.

Would you have a clue where I should look to fix the issue ? Where might it come from ?

Upvotes: 2

Views: 1670

Answers (1)

hynekcer
hynekcer

Reputation: 15548

The problem is caused by an invalid type of default value in a ForeignKey:

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

You should usually not assign a default value to a foreign key, eventually a value compatible with the related primary key and never assign an invalid type like datetime.

The source of field has been deduced from the last AddField in the migration. A such unclear error message seems a special case with UUIDField and Django >= 1.9. For your interest, nothing was checked or adjusted in Django 1.8 and an invalid UUID could be saved. With an implicit primary key id, an error message is clear: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'. Nothing needed to be reported to Django.

Upvotes: 3

Related Questions